Harry
Harry

Reputation: 1472

friend class with inheritance

If I have two Classes as follows with inheritance:

class A
{
    ...
}

class B : public A
{
    ...
}

And a third class with defined as a friend class A:

class C
{
    friend class A;
}

Will I be able to access from class B (which is also an object of type A) all members of class C as if I had defined class B the friend Class in the first place?

Upvotes: 12

Views: 9989

Answers (8)

dyomas
dyomas

Reputation: 720

No, you can't call C class methods directly, but you can access them by pointer. A class should be modified:

class C
{
  void method()
  {
  }
  friend class A;
};

class A
{
protected:
  constexpr static void (C::*f)() = &C::method;
};

class B : public A
{
  B()
  {
    //C().method(); ← This wont work
    (C().*f)(); // ← This works fine
  }
};

Accessing data members and static data are simple too

Upvotes: 0

Viral Shah
Viral Shah

Reputation: 71

One more example adding to Masked Man's answer/code above that I found useful:

class B {
  friend class F;
  friend class E;
  int Taurus;
};

class E : public B {
  int Egg;
};

class F {
    void Foo () {
       B b;
       b.Taurus = 4;  //Works F is friend of B (of course)
       E e;
       e.Taurus = 6; // Works E is derived from B and F is friend of B 
                  // Taurus is private member of B.
       e.Egg = 5; // Does not work, F is friend of Base Class B but not
               // E, so can't access members of the derived class
    }
};

Upvotes: 2

v010dya
v010dya

Reputation: 5858

For some reason everybody has forgotten that you can access virtual private functions of a class that derives from the friendship-giver.

#include <iostream>

class Friend;
class Parent
{
    friend class Friend;
private:
    virtual void nameYourself() { std::cout << "Parent" << std::endl; }
};

class Child : public Parent
{
private:
    virtual void nameYourself() { std::cout << "Child" << std::endl; }
};

class Friend
{
public:
    void foo(Parent *p) { p->nameYourself(); }
};

int main()
{
    Parent p;
    Child c;
    Friend f;
    f.foo(&p);
    f.foo(&c);
    return 0;
}

The output of running above code is:

Parent
Child

The reason why it works is tricky and has to do with how this pointer is passed (look up vtables). Were you to remove "virtual" keyword from the function declaration and you will lose this ability.

Upvotes: 6

A B
A B

Reputation: 4158

  • No - it is not inherited (see below); if B is a subclass of A and C is a friend of A, B doesn't have access to C's private members, including the inherited members.
  • Likewise, if A is a friend of C, or if both A and C are mutually friends of each other, this does NOT give B access to C's private members, including the inherited members.
  • This URL states the subclasses of friend classes don't inherit the friend associations:

    C++ friend inheritance?

  • This applies to BOTH "associations (both the main class' own and other classes friended with the main class)" - the question is for the latter case here.

Upvotes: 1

Masked Man
Masked Man

Reputation: 11095

friendship is neither inherited nor transitive. It is strictly one-one relationship between two classes.

class A {
  friend class B;  
  int Aries;
};

class B {
  friend class C;  
  int Taurus;
};

class C {
  int Leo;
  void Capricorn() {
    A a;
    a.Aries = 0;  // this wont work, C is not a friend of A.
                // friendship is not transitive
  }
};

class D : public C {
  void Gemini() {
    B b;
    b.Taurus = 0;  // this wont work, D is not a friend of B.
                   // friendship is not inherited
  }
};    

class E : public B {
  void Scorpio() {
    C c;
    c.Leo = 0; // this wont work either, friendship is not inherited
  }
};

Reference: "The C++ Programming Language" Bjarne Stroustrup

More explanation (mine): If friendship were not one-one, it would be the end of encapsulation. Note that B class can access private members of A only if the class declaration of A declares B as friend. B cannot enforce friendship on A.

Now, if friendship could be inherited, then someone just needs to inherit B to access private members of A, without A having any say in preventing it. Also, allowing friendship to be transitive would lead to other problems, since now B could have a friend C, who in turn could have a friend D, all the way to Z. All of B, C, D, ..., Z can now access A's private members, which would be a disaster.

Upvotes: 27

Mike Seymour
Mike Seymour

Reputation: 254771

To quote the standard, C++11 11.3/10:

Friendship is neither inherited nor transitive.

Meaning that neither derived classes of friends nor friends of friends receive the benefits of friendship.

Upvotes: 3

Daniele
Daniele

Reputation: 831

I didn't understand what you are trying to do, but B is a superclass of A. Is not an object. Between B and C there isn't any inheritance relationship

Upvotes: 0

phaazon
phaazon

Reputation: 2002

I think it depends. You will have access from the A part of B (the sliced part). If you’ve defined B’s own function, I think you won’t.

Upvotes: 0

Related Questions