I know Bruce Eckel's C ++ Thinking is not a reference, but I found a strange paragraph, and I donβt understand, it applies today:
Creating a nested structure does not automatically give private members access to it. To achieve this, you must follow a specific form: first, declare (without definition) a nested structure, then declare it as a friend and, finally, define the structure. the definition of the structure must be separated from the declaration of the friend, otherwise it would be seen by the compiler as non-member.
I really tried this without declaring the nested structure as a friend, and it worked:
struct myStruct{ private: int bar; public: struct nestedStruct{ void foo(myStruct *); }a; }; void myStruct::nestedStruct::foo(myStruct * p){ p->bar = 20; }
Is there a need to declare each other a nested structure for changing private members of the base class?
source share