Redian新闻
>
A C++ private member function problem
avatar
A C++ private member function problem# Programming - 葵花宝典
lz
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!
avatar
w*m
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)
: {

avatar
lz
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

avatar
o*o
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)
: {

avatar
lz
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.
avatar
o*o
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?

avatar
X*r
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?

avatar
r*r
8
看看这个function里面的this指针是不是这个class

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

avatar
c*n
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)
: {

avatar
c*n
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.
avatar
t*t
11
I guess that that access control is based on class instead of object has to
do with pointer analysis problem.
avatar
t*t
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.

avatar
c*n
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?

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。