14

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?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
tunnuz
  • 23,338
  • 31
  • 90
  • 128
  • Yet another question that should have been answered easily using a COMPILER. – shoosh Feb 28 '09 at 11:39
  • 9
    @shoosh: compilers do not necessarily conform to standards. Such a question should be answered by the standard, not compilers. – Hosam Aly Mar 01 '09 at 07:58
  • @shoosh Which compiler? VS for windows or g++ for unix? Which version of C++ standard? There's many questions you can ask even if you compiler works or doesn't work. – FreelanceConsultant Aug 23 '15 at 17:56
  • Perhaps a good question to ask now is can you make it such that friendship is inherited? It would certainly be useful for what I am doing rather than writing out a list of all the classes I want to have "friendship". – FreelanceConsultant Aug 23 '15 at 17:57

3 Answers3

19

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
  • 35,488
  • 12
  • 65
  • 73
  • 6
    It's interesting that you chose the exact wording to say that as this website: http://www.cplusplus.com/doc/tutorial/inheritance/ – dicroce Apr 24 '09 at 20:48
  • Perhaps a good question to ask now is can you make it such that friendship is inherited? It would certainly be useful for what I am doing rather than writing out a list of all the classes I want to have "friendship". – FreelanceConsultant Aug 23 '15 at 17:57
9

No it isn't.

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

Friendship is neither inherited nor transitive.

7

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

Daniel Bruce
  • 11,269
  • 4
  • 30
  • 28
  • 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 Feb 28 '09 at 11:06