Quick Question about Nested Classes and Freind Scope
In the code below ...
Visual Studio SP1 compiles OK
Visual Studio without Sp1 throws an error
'can't access private variable bar'
The Question is is SP1 wrong?
I don't think A.bar should be available to the nested Class C, does anyone know if the C++ spec allows this?
Thx
wc
class B;class A { friend class B; private: int bar; }class B{ public: class C { A myclassA; foo () { int y = myclassA.bar; // error in pre SP1 visual studio } }}
Actually, to the letter of the C++ standard, SP1 is wrong (at least as I interpret the standard).
The standard does not say anywhere that private members of A should be accessible from a nested class C of friend class B. It only says that (obviously) friend class B (including its member functions and static data members) should have access to A's privates.
That does make sense too, since the B::C class was not declared a friend of the A class, so why should it have access to its privates ?
Consider (as a side note) that a class doesn't even have access to the privates of a nested class :
class A { public : void fun1() { class B b; b.mb; } // OOPS : A::B::mb is private void fun2() { class C c; c.mc; } // OK : A is a friend of A::C public : class B { private : int mb; }; class C { private : int mc; friend class A; };};
infinity's comment reiterates what i pointed out through my links....private members cannot be accessed anywhere outside their class other than of course by their friends