Youyuan functions and youyuan classes

Source: Internet
Author: User
Youyuan functions and youyuan classes

Data is hidden and encapsulated using the class mechanism. Data members of the class are generally defined as private members, and member functions are generally defined as common, provides communication interfaces between classes and the outside world. However, sometimes some functions need to be defined. These functions are not part of the class, but need to frequently enumerate the data members of the class. In this case, these functions can be defined as the friend functions of the function. In addition to the youyuan function, there are also youyuan classes, both collectively referred to as youyuan. Youyuan's role is to provide the operation efficiency of the process, but it damages the encapsulation of the class.

1. youyuan Function

A friend function is a non-member function that can directly invoke private members of a class. It is a common function defined outside the class. It does not belong to any class, but needs to be declared in the class definition. When declaring it, you only need to add the keyword friend before the name of the youyuan, the format is as follows: function name of the friend type (form parameter). The declaration of a friend function can be placed in the private or public part of the class. There is no difference between them, it indicates a friend function of the class. A function can be a membership function of multiple classes, but must be declared separately in each class of brother. The method and principle of calling a friend functions are the same as that of a common function.

2. youyuan class

When you want a class to be able to access the private members of another class, you can declare this class as another type of membership class. The statement format for defining a friend meta-class is as follows: Friend class name. Among them, friend and class are keywords, and the class name must be a defined class in the program. For example, the following statement indicates that Class B is a friend class of Class A: Class {... Public: Friend Class B ;...}; After the preceding descriptions, all member functions of Class B are membership functions of Class A and can access private and protected members of Class. Note:

(1) the relationship between friends and Meta is not symmetric. That is, if Class B is a friend of Class A, it does not imply that Class A is a friend of Class B.

(2) The relationship between friends and Meta is not passed. That is, if Class B is a friend of Class A, and class C is a friend of Class B, this does not imply that class C is a friend class of Class.

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.