C + + Learning Summary of six: friend function and friend class

Source: Internet
Author: User

After adopting the class mechanism, the data hiding and encapsulation are realized, the data members of the class are generally defined as private members, and the member functions are generally defined as common, which provides the communication interface between the class and the outside. However, there are times when you need to define functions that are not part of a class, but require frequent access to the data members of the class, and you can define these functions as friend functions for that function. In addition to the friend function, there are friends of the class, the two collectively referred to as friends. The function of the friend is to provide the operation efficiency of the operation, but it also destroys the encapsulation of the class.

1. Friend function

A friend function is a non-member function that can directly access a private member of a class. It is a normal function that is defined outside of a class, and it does not belong to any class, but it needs to be declared in the definition of a class by adding the keyword friend to the name of the friend, in the following format:

Friend type function name (formal parameter);

The declaration of a friend function can be placed in the private part of the class, or it can be placed in the public domain, and they are indistinguishable, all of which indicate a friend function of the class.

A function can be a friend function of multiple classes, but it needs to be declared separately in the various classes of the Gothic.

The invocation of a friend function is consistent with the way and principle that a general function is invoked.

2. Friend class

When you want a class to be able to access a private member of another class, you can declare the class as a friend of another category. The statement format that defines a friend class is as follows:

Friend class class name;

Where: friend and class are keywords, and class names must be a defined class in the program.

For example, the following statement shows that Class B is a friend of Class A:

Class A

{

...

Public

Friend class B;

...

};

After the above description, all member functions of Class B are friend functions of Class A, and can access private members of Class A and protect members.

Attention:

(1) The friend relation does not have the symmetry, namely if Class B is the friend class of Class A, does not imply that class A is a friend class of Class B.

(2) The friend relationship is not transitive, that is, if Class B is a friend class of Class A, and Class C is a friend class of Class B, this does not imply that Class C is a friend class of Class A.

Related articles

C + + Learning Summary bis: constructors and destructors

Http://www.bianceng.cn/Programming/cplus/200911/12302.htm

One of the C + + Learning Abstracts: Classes and objects

Http://www.bianceng.cn/Programming/cplus/200911/12301.htm

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.