C ++ Friend functions (Friend)

Source: Internet
Author: User

Outside the class, the private or protected members of any instance category class are forbidden, which is out of security considerations. However, in many cases, for the sake of practicality, we do need to use the private or protected member of the external entity class, which can be implemented by the keyword friend in C ++. The friend mechanism allows a class to grant access to its non-public members (private and protected members) to the specified function or class. Youyuan functions must be declared inside the class. youyuan is not the class member that grants the youyuan relationship. For the sake of encoding style, generally, the Union Declaration Orange group is placed at the beginning or end of the class definition. 1. The global function is used as the Union [cpp] // Friend_Global.cpp # include <iostream> using namespace std; class Base {int value; // Private member protected: string str; // protected member public: Base (const string newStr, const int ii): str (newStr ), value (ii) {} friend void view (Base &); // declare the global function of friend}; void view (Base & B) {// accept the base class Object cout <"string =" <B. str <endl <<"Value =" <B. value <endl; return;} int main () {Base B ("Base", 49); view (B);/* // any instance, the private or protected members that attempt to access the // class outside the class are all prohibited cout <"string =" <B. str <endl <"value =" <B. value <endl; */return 0;} running result: it is worth noting that the global function void view (Base & B) is modified using the friend keyword) it is not a member function of the Base class. It is only a global function. 2. The member functions of other classes become friends [cpp] // Friend_Class.cpp # include <iostream> using namespace std; class Y; // view in Class X Y appears in the function. Therefore, the class Y class X {public:/* parameter type must be declared as a pointer in advance, because the class Y is defined below, before the definition of Y, the compiler may not know how much memory space should be allocated to the Y object. If it is a Y * pointer, the compiler makes an incomplete type match and the compiler knows how to pass an address, this address has a specific size, regardless of the object to be passed, even if it does not fully know the size of this object type, although in fact it is also possible to pass the object, however, in this case, it is safer to pass an object address! */Void view (const Y *);}; class Y {int value; // Private member protected: string str; // protected member public: Y (const string newStr, const int ii): str (newStr), value (ii) {} friend void X: view (const Y *); // declare the global function of friend}; void X:: view (const Y * y) {cout <"string =" <y-> str <endl <"value =" <y-> value <endl; return;} int main () {Y y ("Base", 49); X x; X. view (& y); return 0;} running result:

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.