Friendship is not inherited

Hello,

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 :) whether it makes any sense from OO perspective but I still want to know what is the expected outcome is from C++ perspective.

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 in any way meant to be a pattern that makes any sense OO-wise.
You need to read some more on inheritance. I recommend you over to

the C++ FAQ-Lite
http://www.parashift.com/c++-faq-lite/

There are huge sections on public inheritance.

There is also a very good section on protected and private inheritance. (Which, BTW, don't work the same was as public inheritance!)


As for not allowing friendship to be inherited, that is to protect encapsulation. Otherwise foreign objects have intimate access to your internals -- which is a Bad Thing.

Hope this helps.
Thanks Duoas for trying to help!

Unfortunately, none of the above guidelines answer any of my posted question directly or indirectly (regardless if they are useful or not). Obviously, none of the 3 types of inheritance work in the same way - otherwise, there would not be 3 of them (no c++ knowledge is required to come to such conclusion).

I happen to have read many of the nice FAQs on parashift (including the inheritance section and friendship one) but I still have the posted question.
Well, that's a pretty bold stance to take. I'm not in the habbit of not answering questions directly or indirectly (regardless of fun factor). Good luck!
I thought I could shed some light -- I'm doing similar experiments with children of the friend -- but the results of this experiment baffle me. I don't know the C++ standard well enough to know what should happen, but this scheme seems so simple and basic that I can't believe the behavior is undefined. By experiment (with gcc 3.4.4) the friendship exposes all of a public parent, the non-private members of a protected parent and none of a private parent.

I can't see how this makes sense. By accident or design, this "compromise" pattern won out over simpler alternatives.
Hello Beta,

Thanks for you help. Btw, I asked the question on another forum (http://www.cpp-home.com/forum/viewtopic.php?f=4&t=16513). You may find some of the info there interesting (suggesting a compiler issue not present in other compilers) but it doesn't go much beyond what you already indicated, so doesn't resolve our confusion but at least sheds some light on how different compilers behaved.

I also asked this through www.parashift.com/c++-faq-lite/ a while back but have not heard a response.
Topic archived. No new replies allowed.