Youyuan relationship and inheritance-notes in C ++ primer, a small bug in the book

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.