Explanation of the relationship between C ++ and youyuan

Source: Internet
Author: User

In C ++, in some cases, it is convenient to allow a specific non-member function to access a private member of a class while still blocking normal access. For example, an overloaded operator, such as an input or output operator, often needs a private data member of the operator class. These operators cannot be members of a class. However, although they are not members of a class, they are still the "interface components" of the class ". Youyuan allows a class to grant access to its non-public members to a specified function or class. The statement of youyuan starts with the keyword "friend. It can only appear within the class definition. A friend statement can appear anywhere in the class: A friend is not a member of the class that grants a friend relationship, so they are not affected by Access Control in part of the statement. The following are all the conclusions and restrictions on user Meta in C ++, and examples are given for each detail: 1. There are three types of user meta relationships: 1.1. A non-member function is reset () declared as a friend function of the example Class, so that this non-member function can be a private member of the example class. [Cpp] class example; // The class example must be declared in the forward direction. Otherwise, the following function declaration will report the error void reset (example & e); class example {public: friend void reset (class example & e); private: int n ;}; // The function definition must be placed behind the class example, otherwise, the variable n void reset (example & e) {e. n = 0;} 1.2, declares the class man as a woman-like user Meta class, so that you can use the class man object to declare the private member of the woman class. [Cpp] class woman; // class man {public: void disp (woman & w); void reset (woman & w) ;}; class woman {public: friend class man; // set man to woman's friend class, so that any member function of the man object can access woman's private member private: string name ;}; void man :: disp (woman & w) {cout <w. name <endl;} void man: reset (woman & w) {w. name. clear ();} 1.3: declares a member function of class Y as a friend function of Class X, so that you can use this member function of class Y to access a private member of class X. [Cpp] class woman; // class man {public: void disp (woman & w); void reset (woman & w) ;}; class woman {public: friend void man: disp (woman & w); // set one of man's member functions disp () to woman's friend function, you can use this function to access the private member of the woman object. private: string name ;}; void man: disp (woman & w) {cout <w. name <endl;} // The reset () member function of man is not a woman function, so it cannot access its private member/* void man: reset (woman & w) {w. name. clear ();} */2,

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.