Friend classes, methods and benefits of C + +

Source: Internet
Author: User
Tags visibility

In Java, we know that there are default package visibility access levels in addition to public and private,protected, though, many times for reasons of early design flaws, we need to access some packages or protected the visibility level. This time is more troublesome, if the choice and target service in the same package, always look strange, if it is inherited as a subclass, it is even more strange. However, we do not want to declare the visibility of the interface service as public.

In C + +, this is better, we know that in C + +, there is a friend class, the characteristics of the method, which is equivalent to the role of the white list, when a class or member function, non-OO function is declared as a friend of the target class, it can access any member including private familiar, function. As shown below:

#pragmaOnceclassbase{intJ; Public:    intI//for testing convenience onlyBase (void); ~base (void); Virtual voidTestdyncast (); FriendvoidGlobalfriend ();//friend functionFriendclassFirstcppcls;//Friend class};
" stdafx.h "  "FirstCPPCls.h " "Base.h"  Firstcppcls::firstcppcls (void) {    Base b;     1 ;}

In this way, the visible range can be controlled exactly as needed.

Other can refer to the next http://blog.chinaunix.net/uid-790245-id-2037327.html

Friend classes, methods and benefits of C + +

Related Article

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.