문제

I just discovered that I can do this in C++, which compiles just fine (MinGW and VC++):

class A
{
private:
   void doSth();
   A* foo;
   A* bar;
};

void A::doSth()
{
  foo->bar;
}

The member field bar of foo is private. Why can I access it and should I do so?

도움이 되었습니까?

해결책

In C++, private means the access is limited to the same class, not to the same object instance.

다른 팁

You'll also notice that in A's methods, other instances of A's private variables may be access. Like

A::baz(A& other) {
    other.bar; // legal
}

Visibility is at the class level, not instance level.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top