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?
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.
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
One more example adding to Masked Man's answer/code above that I found useful:
For some reason everybody has forgotten that you can access virtual private functions of a class that derives from the friendship-giver.
The output of running above code is:
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.
To quote the standard, C++11 11.3/10:
Meaning that neither derived classes of friends nor friends of friends receive the benefits of friendship.
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.