由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - A C++ private member function problem
相关主题
[合集] 正经问个代替Matlab的问题。 (转载)
相关话题的讨论汇总
话题: private话题: init话题: foo话题: void话题: class
进入Programming版参与讨论
1 (共1页)
lz
发帖数: 23
1
Hello,
I am confused by the following code.
class A{
private:
void init();
public:
void foo(A& a);
}
void A::foo(A& a)
{
a->init();
}
This code passes c++ compliling. My question is that init()
is a private function, how can that be called in foo()?
Thank you!
w****m
发帖数: 146
2
foo is the member function of A
so when init is called in foo, the scope is still in A

【在 lz 的大作中提到】
: Hello,
: I am confused by the following code.
: class A{
: private:
: void init();
: public:
: void foo(A& a);
: }
: void A::foo(A& a)
: {

lz
发帖数: 23
3
But in foo, actually A does not call its own version of init(), it calls a->
foo().
That is what I am confused at.

【在 w****m 的大作中提到】
: foo is the member function of A
: so when init is called in foo, the scope is still in A

o*o
发帖数: 404
4
private is relative to other functions and classes, not itself.

【在 lz 的大作中提到】
: Hello,
: I am confused by the following code.
: class A{
: private:
: void init();
: public:
: void foo(A& a);
: }
: void A::foo(A& a)
: {

lz
发帖数: 23
5
get it. private is relative to the classes, not the instances of classes.
Am I right?

【在 o*o 的大作中提到】
: private is relative to other functions and classes, not itself.
o*o
发帖数: 404
6
no. I'd say its m.f.

【在 lz 的大作中提到】
: get it. private is relative to the classes, not the instances of classes.
: Am I right?

X****r
发帖数: 3557
7
Yes, in C++ access control is based on classes (and functions), not objects.

【在 lz 的大作中提到】
: get it. private is relative to the classes, not the instances of classes.
: Am I right?

r****r
发帖数: 115
8
看看这个function里面的this指针是不是这个class

【在 lz 的大作中提到】
: get it. private is relative to the classes, not the instances of classes.
: Am I right?

c*********n
发帖数: 128
9
Huh, that's interesting. I cannot understand either.
I revised the code a little bit to make the problem even more obvious:
//**************************code ******************************************
#include
using namespace std;
class A{
private:
string name;
private:
void init();
public:
A(string name):name(name){}
void foo(A& a);
};
void A::foo(A& a) {
a.init();
cout << "The parameter's name is " << a.name < }
void A::init() {
cout << "This is " << name << endl;
}
int main()

【在 lz 的大作中提到】
: Hello,
: I am confused by the following code.
: class A{
: private:
: void init();
: public:
: void foo(A& a);
: }
: void A::foo(A& a)
: {

c*********n
发帖数: 128
10
This sounds very weird for me. what's the point to use access control based
on class instead of object?
If a1 and a2 are both of Class A, then the member functions of a1 have
access to the private data of a2?
That's ridiculous for me......

objects.

【在 X****r 的大作中提到】
: Yes, in C++ access control is based on classes (and functions), not objects.
t*******t
发帖数: 6
11
I guess that that access control is based on class instead of object has to
do with pointer analysis problem.
t****t
发帖数: 6806
12

based
So, what's the point of object based access control? What's the advantage
over class based access control? What other OO language, if any, use object
based access control?
That's right. To be more precise, the member functions of A have access to
the private data of both a1 and a2.
May i ask why it is ridiculous, for you?

【在 c*********n 的大作中提到】
: This sounds very weird for me. what's the point to use access control based
: on class instead of object?
: If a1 and a2 are both of Class A, then the member functions of a1 have
: access to the private data of a2?
: That's ridiculous for me......
:
: objects.

c*********n
发帖数: 128
13
I had never paid attention for this but just intuitively thought the access
control was based on object......but......
I just rewrote the test code in Jave..... and it's true that access control
is based on class.
However, isn't it a danger that all the member functions of Class A have
access to the private data of any objects of A?
public class A {
private String name;

private void init() {
System.out.println("This is " + name);
}

public A(String name) {


【在 t****t 的大作中提到】
:
: based
: So, what's the point of object based access control? What's the advantage
: over class based access control? What other OO language, if any, use object
: based access control?
: That's right. To be more precise, the member functions of A have access to
: the private data of both a1 and a2.
: May i ask why it is ridiculous, for you?

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 正经问个代替Matlab的问题。 (转载)
相关话题的讨论汇总
话题: private话题: init话题: foo话题: void话题: class