skydoor skydoor - 3 years ago 122
C++ Question

friend function in derived class with private inheritance

If a class Derived is inherited privately from a class Base and the Derived class has a friend function f(), so what members can f() access from Derived class and Base class.

class Base {
int a;
int b;
int c;

class Derived: private Base {
void friend f() {}

int d;
int e;
int f;

I understand that if a class is inherited privately from the base class, everything is private in the derived class.

But why in the code above, the function f() can access a, b, d, e, f but not c?

Answer Source

'Friendship' grants access to the class that declares the friend - it's not transitive. To use a bad analogy - my friends are not necessarily my dad's friends.

The C++ FAQ has a bit more detail:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download