About the friend function of C + +

Source: Internet
Author: User

Suppose there is such a class

1 class AA 2 {3     Private : 4       int num; 5       string name; 6       Public : 7      AA (intint  b); 8 }

At this point, if a function of the outside (void display ()) wants to access the member variable of the name of the Class AA, the function may not belong to either class or other class. It is obviously impossible because he is a private member of the class and only the member functions of that class can access

But at this point, you have to access this variable, you can declare this function as the friend function of this class, as follows

1 classAA2  {3      Private:4       intnum;5       stringname;6        Public:7AaintAintb);8FriendvoidDisplay ();//declaring a friend function9}

This time the display function can access the private members of the AA class, not to mention the public members

The above said more clearly, the following to summarize:

1 A friend function is one that allows a class to grant access to its non-public members to this function

2 The declaration of a friend relationship can only appear anywhere in the class, but the friend function is not a member function of the class and is not affected by the access control, so it is common to place the declaration of the friend at the beginning or end of the class.

3 friends can be normal functions, or member functions of other classes that have been defined, or the entire class.
Set a function as friend, called friend function, and a
Class is set to friend, called Friend class. All member functions of the friend class can access the non-public members of the class that grants the friend relationship.
Therefore, access to a non-public member of a class can have two users: class member and friend

4 friend relationships are one-way rather than two-way. If Class B is declared to be Class A
A friend class, not equal to Class A is a friend of Class B, the member function in Class A cannot access the class
Private data in B.
5 friend relationships cannot be passed or inherited, if Class B is a friend class of Class A, Class C
is a friend of Class B, not equal to Class C is the friend class of Class A. If you want Class C to be Class A,
Friend class, which must be explicitly declared in Class A separately.

The most important point:

Generally do not UF meta-function

About the friend function of C + +

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.