This is my first time to post a question here.
class Base {
private:
int base;
friend class Question;
};
class Derived : public Base{
private:
int super;
};
class Question{
public:
void test(Base& base, Derived & derived)
{
int value1 =base.base; // No problem, because Question is a friend class of base
int value2 =derived.super; // Compile error, because Question is not a friend class of base
// Question is here
int value3 =derived.base; // No Compile error here, but I do not understand why.
}
};
The question is indicated in the last row of Class Question.
friend
applies to all of the members of that type, whether or not the type is inherited. To stress this point: it is the members that are shared.
This means that your Question
class has access to all members of Base
, which is just int Base::base
. Whether it accesses this member through an instance of Derived
is irrelevant, because the actual member being accessed was declared on Base
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With