Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance and Friendship access. C++

I have the following query;

classB inherits from classA 
classC is friend of classB

Doesn't this mean classC should be able to access protected member of classA? Since classB inherits this from classA, an classC can access everything in class classB?

like image 604
user1005253 Avatar asked Apr 05 '12 20:04

user1005253


People also ask

Is friendship inherited in C++?

Difference between Inheritance and Friendship in C++: In C++, friendship is not inherited. If a base class has a friend function, then the function doesn't become a friend of the derived class(es).

What is access mode in inheritance?

This means that we have created a derived class from the base class in public mode. Alternatively, we can also derive classes in protected or private modes. These 3 keywords ( public , protected , and private ) are known as access specifiers in C++ inheritance.

Can friend function access private members of inherited?

A friend class can access both private and protected members of the class in which it has been declared as friend.

Is friendship inherited?

The answer is very simple: no, subclasses do not inherit friend associations. A friend can only access the private members of the class the association is declared in, not those of parents and/or children of that class.


2 Answers

[My original answer was nonsense. Apologies for that. Thank you to @celtschk for pointing that out and providing the better answer.]

If C is a friend of B, it can access all of B's members, whether private, public, or protected, and that includes the accessible (public and protected) members that are part of a base subobject:

struct A { protected: int a; };
struct B : A { private: int b; friend struct C; }

struct C
{
    B x;
    A w;

    void f()
    {
        x.a = 1; // fine
        x.b = 2; // fine

        // w.a = 0; /* Error, #1 */
    }

    friend struct D; // see below
};

However, friendship is neither transitive nor inherited: C is a friend of B, but not of A (see #1). Also, if D is a friend of C, then D doesn't get any of the access that C's friendship to B affords it, so D cannot access B's non-public members. Similarly, if struct E : C inherits from C, then E is also not a friend of B automatically:

struct D
{
     B y;
     void g()
     {
         // y.b = 3; /* Error! */
     }
};

struct E : C
{
     B z;
     void h()
     {
         // y.b = 4; /* Error! */
     }
}

Perhaps one can summarize what's going on in a few points:

  • A derived class has access to all public and protected members of each base class.

  • A friend of a class has access to all members of that class that are accessible to it (i.e. all members excluding private base members).

  • Friendship is not inherited: If a class has a friend, that friendship does not apply to any of its base classes nor to any of its derived classes.

  • A friend of a friend is not a friend.

like image 182
Kerrek SB Avatar answered Sep 22 '22 23:09

Kerrek SB


It means that classC should be able to access the protected classA subobject part of classB. It should not be able to access anything non-public from classA itself.

For example:

class C;

class A
{
protected:
  int i;
};

class B:
  public A
{
  friend class C;
};

class C
{
public:
  void foo(A& a, B& b)
  {
    // a.i = 3; // not allowed
    b.i = 3; // allowed, accesses the `i` of the `A` subobject of `B`
  }
};
like image 43
celtschk Avatar answered Sep 19 '22 23:09

celtschk