C + + Learning Note (16): Friend

Source: Internet
Author: User

The question is raised:

We already know that classes have the properties of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as public, this destroys the hidden features. In addition, it should be seen that in some cases, especially when multiple calls are made to certain member functions, time overhead is required due to parameter passing, type checking, and security checking, which affects how efficiently the program runs.

A friend is a normal function that is defined outside the class, but he needs to be described in the class, in order to distinguish it from the member functions of the class, to be the keyword friend before the description. A friend is not a member function, but he can access a private member in a class. The role of friends is to improve the efficiency of the program, but he destroys the encapsulation and concealment of the class, allowing non-member functions to access the private members of the class.

C + + provides three ways to implement friend relationships, friend function, friend member function, friend class.

Friend function:
Both a common function is described as a friend attribute in a class, and the definition (most of the members that will access the class) should be behind the class;

Friend member functions:
Since it is a member function, then it is certain that this function belongs to a class, because this function is a member function of another class, sometimes because we want to use a class to access the information of another class through an interface, but this information can only be accessed by the class to which it is authorized, then you need to use friend to implement ; This concept only changes slightly at the time of Declaration;

Friend class:
A friend class declaration will describe the whole class as a friend of another class, and the difference between the first two is the difference between the collective and the individual; All member functions of a friend class can be friend functions of another class;

Example:

1 using namespacestd;2 3 classB;4 5 classa{6     Private:7         intx;8      Public:9 A ();Ten         voidDisplay (B &); One }; A  - classC; -  the classb{ -     Private: -         inty; -         intZ; +      Public: - B (); +Bint,int); AFriendvoidA::d isplay (B &);//Friend member function atFriendvoidDisplay (B &);//friend function -FriendclassC//Friend class - }; -  - classc{ -     Private: in         intsum; -         voidCalc (B &); to      Public: + C (); -         voidDisplay (B &); the }; *  $ //must be defined after the class of a friend relationshipPanax Notoginseng voidDisplay (B &v)//friend function - { thecout << v.y <<" "<< v.z <<Endl; + } A  the a::a () + { -      This->x =0; $ } $  - voidA::d isplay (B &v)//Friend member function - { the      This->x = V.y +v.z; -cout << This->x <<Endl;Wuyi } the  - b::b () Wu { -      This->y =0; About      This->z =0; $ } -  -B::B (intYintz) - { A      This->y =y; +      This->z =Z; the } -  $ c::c () the { thesum =0; the } the  - voidC::d isplay (B &v) in { the      This-Calc (v); thecout << sum <<" = "<< v.y <<" + "<< v.z <<Endl; About } the  the voidC::calc (B &v) the { +sum = v.y +v.z; - } the Bayi intMain () the { the A; -b b (2,3); - display (b); the A.display (b); the c C; the C.display (b); the      -     return 0; the}

C + + Learning Note (16): Friend

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.