Friendship, Friendship cannot inherit. What I want to express in the book is that this kind of friendship cannot inherit two-way.
That is to say, a friend's son cannot access himself or his son. The only association is between his friend and himself.
However,The Code expression in the book is indeed wrong. If my thoughts are wrong, please correct me ............
The test code is as follows:
// The derived class cannot inherit the relationship between friends and friends. It is bidirectional and neither can be accessed nor be accessed. # include <iostream> class base {friend class frnd; protected: int I ;}; // frnd has no access to members in d1class D1: public base {protected: Int J ;}; class frnd {public: int MEM (Base B) {return B. i;} // OK: frnd is friend to base int MEM (d1 d) {return D. I ;}// this is incorrect in the book. Description: Friendship doesn't inherit // int MEM (d1 d) {return D. j;} // The fact is that this is wrong. Friendship does not have inherit, But I is a derived class. }; // D2 has no access to members in baseclass D2: Public frnd {// The access permission is not inherited from public: // int MEM (Base B) {return B. I ;}// 'int base: I 'is protected,}; int main () {D1 D; base * B = & D; d2 D2; frnd * F1 = & D2; // print the same information. The error message is returned. Access D. i'm right, D. J is inaccessible, indicating that the friend relationship does not inherit, but the base class in the derived class can still be accessed. // Print the same STD: cout <d2.mem (* B) <STD: Endl; STD: cout <d2.mem (d) <STD: Endl; // The above d2.mem () is inherited from frnd, indicating that the inherited base class also has the privilege to access the base. // In summary, although the two-way relationship between friends and Yuan cannot be inherited, the inherited base class reserves the original power}
Friendship cannot be inherited, and you cannot access the derived class, but the base class in the derived class is indeed open for access.The base class of the symmetric youyuan derived class also retains this access privilege.