Youyuan functions and youyuan classes

Source: Internet
Author: User

data is hidden and encapsulated using the class mechanism. Data members of a 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 improves the running efficiency of the Program (that is, it reduces the time overhead for both type check and security check ), but it destroys the encapsulation and hiding of classes, so that non-member functions can invoke private members of the class.

youyuan function :
A friend function is a non-member function that can directly access private members of the member 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 (formal parameter );

you can place the declaration of functions in the private or public part of the class. They are the same. They all indicate that they are a friend function of the class.
A function can be a membership function of multiple classes. You only need to declare it in each class.
the method and principle of calling a friend functions are the same as that of a General function.

Youyuan class :
All member functions of the youyuan class are the youyuan functions of the other class and can access the hidden information (including private and protected members) in the other 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:
Class
{
...
Public:
Friend Class B;
...
};
after the preceding descriptions, all member functions of Class B are membership functions of Class, can access private and protected members of Class.

note when using a friend class:
(1) Friendship cannot be inherited.
(2) the relationship between friends and friends is unidirectional and not interchangeable. if Class B is a friend of Class A, Class A is not necessarily a friend of class B. It depends on whether a corresponding declaration exists in the class.
(3) the relationship between friends and friends is not passed. if Class B is a friend of Class A and Class C is a friend of Class B, Class C is not necessarily a friend of Class, it also depends on whether there is a declaration in the 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.