views:

413

answers:

3

Suppose I have a Base class:

class Base {
    friend SomeOtherClass;
};

And there is another (different) class that inherits from Base:

class AnotherClass : public Base {}

Is friendship inherited as well?

Thank you

+15  A: 

In principle, a derived class inherits every member of a base class except:

* its constructor and its destructor
* its operator=() members
* its friends

So, no. Friends are not inherited.

simplyharsh
It's interesting that you chose the exact wording to say that as this website: http://www.cplusplus.com/doc/tutorial/inheritance/
dicroce
+7  A: 

No it isn't, as documented here: http://www.parashift.com/c++-faq-lite/friends.html#faq-14.4

Daniel Bruce
The example in the link shows the opposite case to the OP's question. I'd like to add that `SomeOtherClass` will have access to the `Base` fields and methods inherited in objects of `AnotherClass`.
Hosam Aly
+8  A: 

No it isn't.

Edit: To quote from the C++ Standard, section 11.4/8

Friendship is neither inherited nor transitive.

anon