Victor Bazarov a écrit :
I am not sure it's legal. I would be if the situation were reversed,
i.e. 'Inner' tried accessing 'Outer's private members. Yes, using
'friend' is the simplest work-around.
It's definitely not legal - accessing a private member that is not
yours is not legal C++, even if you "own" the class. That's the whole
principle of encapsulation using an access control mechanism.
There is also no point on making Inner private members public to Outer
using the friend keyword. If Inner is supposed to be hidden from any
class but Outer (ie Inner is defined as private in Outer), just define
Inner members as public. However, I strongly advise you to avoid doing
so, as there is also no point in putting a bunch of public members in a
inner class so that they can be accessed only by the owner - just add
private members to your Outer class then. If you want to create an
inner class that actually have some added value, think about it twice:
what should it expose, and how should it expose it? Remember that an
inner class is just another class, which happens to have a particular
semantic in the sense that it is strongly tied to its Outer class. It
is subject to the same design principles as all the other classes.
Regards,
-- Emmanuel Deloget, Artware