1

There are three classes:

class A
{
    friend I_B;

    protected:

    void* mData;
};

class I_B
{
    void foo() = 0;
};

class B_Impl : public I_B
{

    B_Impl( A* value )
    :
    mData( value->mData ) <--- ERROR
    {

    }

    void foo() { mData->DoSomething() };

protected:

    void* mData;
};

At compile time I get an error in the constructor, that mData is a protected member.

Please explain me please why it happens.

Can I get access to protected members using "friendship" of the base class?

Tevo D
  • 3,291
  • 20
  • 28
kaa
  • 1,195
  • 5
  • 19
  • 36

1 Answers1

5

Friendship is not inherited. If you want B_Impl to be a friend of A you must declare B_Impl as a friend.

Friendship is also not transitive: your friend's friend is not necessarily your friend.

Pete Becker
  • 72,338
  • 6
  • 72
  • 157
  • 1
    A nice link too: http://stackoverflow.com/questions/3561648/why-does-c-not-allow-inherited-friendship – Sergey K. Aug 14 '13 at 13:07
  • Thanks, of course if i use friendship between A and B_Impl all works fine. But the design of the application do not allow to do this. So I will create accessor methods to get particular data. Thanks again for the answer. – kaa Aug 14 '13 at 13:11
  • Sergey K, thanks for the link – kaa Aug 14 '13 at 13:15