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 + +