friend class with inheritance

2019-01-26 08:50发布

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?

8条回答
贼婆χ
2楼-- · 2019-01-26 09:26
  • 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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-01-26 09:29

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

查看更多
男人必须洒脱
4楼-- · 2019-01-26 09:34

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
    }
};
查看更多
迷人小祖宗
5楼-- · 2019-01-26 09:38

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.

查看更多
Emotional °昔
6楼-- · 2019-01-26 09:38

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.

查看更多
狗以群分
7楼-- · 2019-01-26 09:43

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.

查看更多
登录 后发表回答