Can a friend class object access base class private members on a derived class object?
问题 I'm surprised that the code below compiles. It seems that a class befriended to the (publicly inherited) base class can access a member of the base class provided an instance of the derived class. If the inheritance is changed to private then compilation fails. In short, how is d.b_var valid within F::func(D& d) ? #include <iostream> #include <string> using namespace std; class B{ int b_var; friend class F; }; class D: public B{ int d_var; }; class F{ public: void func(D &d){ d.b_var = 5; } }