The common/classical explanation for the statement "c++ friendship is not inherited" is that if class X declares class P as a friend, then children of P are not considered friends of X.
There is another scenario which I don't know (or care
The scenario is that a parent class declares another class a friend. We'll call the latter parentFriend. What access would parentFriend have to members of class parent that are inherited inside various 'types' of children.
Below is the small piece of code demonstrating the relations/question.
I ran the code and know the answer (not posted here, slightly different for 2 different compilers) but I don't quite understand the results I got. I want to know the expected by design behavior (assuming defined).
class parentFriend;
class parent {
friend class parentFriend;
public: void f1(){};
protected: void f2(){};
private: void f3(){};
};
class childpub: public parent {};
class childprot: protected parent {};
class childprv: private parent {};
class parentFriend {
public:
void f(){
/* which of the following 6 statements will compile? why? why not?*/
parent p; p.f1(); p.f2(); p.f3(); // these will
childpub cpub; cpub.f1(); cpub.f2(); cpub.f3(); // (1)
childprot cprot; cprot.f1(); cprot.f2(); // (2)
cprot.f3(); //(3)
childprv cprv;
cprv.f1(); // (4)
cprv.f2(); // (5)
cprv.f3(); // (6)
}
};
Thanks in advance for any hints. This is neither related to any practical problem nor in any way a suggestion the above code is a pattern that makes any sense OO-wise.
