Inheritance and Friendship access. C++

access-control, c++, friend, inheritance

Solution

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`
  }
};

Problem

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?

Original source