First, why the introduction of friend mechanism? In general, to let non-member functions, ordinary functions or other classes can access the private members of the class, which does undermine the encapsulation of the class and data concealment, but
In some cases, it is more convenient to grant member-level access to functions that are not members of a class or to all functions in a separate class. Class-only implementations can declare their friends. A function or class cannot declare itself
Friend functions and friend classes are less used in actual development, and readers who want to learn C + + quickly can skip this section.A class can have members of public, protected, private three properties that can access public members, and
1. The need for friend functions and friend classes:The class has encapsulation and information hiding attributes.Only member functions of a class can access private members of a class, and other functions in the program cannot access private
It is undoubtedly a great advantage of object-oriented programming to encapsulate data with functions of processing data, form classes and realize data hiding. But sometimes the encapsulation is not absolute.
A friend function provides a mechanism
The needs of friend functions and friend classes:Class has an attribute 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.
A class can have members of public, protected, private three attributes that can access public members through objects, and only functions in this class can access private members of this class. Now, let's add an exception--friend.
Fnend means a
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
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
In some cases, it is convenient to allow specific non-member functions to access private members of a class while still blocking general access. For example, overloaded operators, such as input or output operators, often require access to private
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.