Youyuan functions and youyuan classes

Source: Internet
Author: User

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 improves the program running efficiency (that is, it reduces the time overhead required for Type checks and security checks), but it destroys the encapsulation and hiding of classes, this allows non-member functions to invoke private members of the class.

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 the friend functions can be placed in the private or public part of the class. They are no different. 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 common 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 A. They can access private and protected members of Class.

Note:
(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 there is A corresponding declaration in the class.

(3) The relationship between friends and friends is not transmitted. If Class B is A friend of Class A, Class C is A friend of Class B, and class C is not necessarily A friend of Class A, it also depends on whether there is A corresponding statement in the class

Youyuan Function

Features: You can use the meta function to allow private members in the member class. NonMember functions. In terms of syntax, youyuan functions are the same as normal functions, that is, they are defined and called in the same way as normal functions. The relationship between friends and friends is not symmetric. That is, A is B's friend, but B is not necessarily A's friend. The relationship between friends and friends is not passed. That is, B is the friend of A, C is the friend of B, but C is not necessarily the friend of. Functions and features Youyuan provides a mechanism for data sharing between member functions of different classes and between member functions of the class and general functions. With youyuan, a member function in a different function or another class can be a private member or a protected member in the category. In c ++, youyuan opened a small hole to encapsulate and hide this opaque wall, allowing the outside world to peat the internal secrets through this small hole.The correct use of youyuan can improve the running efficiency of the program, but it also damages the encapsulation of classes and data hiding, resulting in poor program maintainability. Application Instance

The following example shows the application of the functions.

# Include
 
  
# Include
  
   
Using namespace std; class Point {public: Point (double xx, double yy) {x = xx; y = yy ;}; void Getxy (); friend double Distance (Point & a, Point & B); private: double x, y ;}; void Point: Getxy () {cout <"(" <
   
    
Note: The Point class in this program describes a friend's meta function Distance (). It adds the friend keyword before the description to identify that it is not a member function, but a friend's meta function. Its definition method is the same as that of a common function, but different from that of a member function, because it does not need to specify the class to which it belongs. However, it can reference. x, B. x,. y, B. y is a private member of a class, which is referenced by an object. When calling a friend function, it is also the same as calling a common function. Do not call it like a member function. In this example, p1.Getxy () and p2.Getxy () are member function calls and are represented by objects. Distance (p1, p2) is the call of a friend function. It is called directly without object representation. Its parameter is an object. (The function of this program is to obtain the distance between two points based on known two-point coordinates .)

3. youyuan class

In addition to the previously mentioned functions, youyuan can also be a class, that is, a class can be used as a friend of another class. When a class acts as a friend of another class, this means that all member functions of this class are friends of another class. Let's review the definition of the overloaded "equal to operator", which is provided for the String class defined in the namespace domain. The "equal to operator" for two String objects is as follows:
123456 booloperator==(constString &str1, constString &str2 ){ if( str1.size() != str2.size() ) returnfalse; returnstrcmp( str1.c_str(), str2.c_str() ) ? false:true;}
Compare this definition with the operator definition defined as a member function:
123456 boolString::operator==(constString &rhs ) const{ if( _size != rhs._size ) returnfalse; returnstrcmp( _string, rhs._string ) ? false:true;}
Do you see the difference? We have noticed that you must modify the reference method for private data members of the String class in the function definition. Because the new equals operator is a global function and is not a class member function, it cannot directly reference the private data member of String. It uses the access member function size () and c_str () to obtain the size of the String object and the underlying C-style String. Another possible implementation is to declare the global "equals operator" as the friend of the String class. By declaring a function or operator as a friend, a class can grant this function or operator the right to access its non-public members. A friend Declaration starts with the keyword "friend". It can only appear in the class definition. Because youyuan is not a member of the authorization class, it is not affected by the public private and protected declared areas of its class. Here we choose to organize all the comments together and put them behind the Class header:
12345678 classString{ friendbooloperator==( constString &, constString & ); friendbooloperator==( constchar*, constString & ); friendbooloperator==( constString &, constchar* );public: // Other parts of the... String class};
The three member declarations in the String class include the three overloaded "comparison operators" declared in the global domain (described in the previous section) since these equal operators have been declared as friends of the String class, their definitions can directly reference the Private Members of the String.
12345678910111213 // The friend operator directly references the private member of the String.// friend operators: refer to String private members directlybooloperator==(constString &str1, constString &str2 ){ if( str1._size != str2._size ) returnfalse; returnstrcmp( str1._string, str2._string ) ? false:true;}inlinebooloperator==( constString &str, constchar*s ){ returnstrcmp( str._string, s ) ? false:true;}// Below
In this case, some may say that c_str () and size () provide equivalent efficiency because they are inline, and the member encapsulation is retained, so it is not necessary to directly access _ size AND _ string. This is correct. Using members to access functions rather than directly accessing members does not always mean that they are less efficient. Because these access functions exist, it is not necessary to declare the equals operator as a friend of the String class. So how can we determine whether a non-class member operator should be a friend of the class or use a member to access the function? In general, the implementer of the class should try to minimize the number of operators represented inside the namespace function and the operator class. If you have provided access to member functions and they have the same efficiency, it is best to use these member functions and isolate the changes in the namespace operators from the class representation. However, if the implementers of A Class decide not to provide access member functions for some private members of the class, and the namespace operator must reference these private members to complete their operations, they must use the membership mechanism. The most common usage of a friend declaration is to allow non-member overload operators to access a private member of a class that regards it as a friend. In addition to providing the symmetry of the left and right operands, non-member overload operators can completely access private members of a class just like member functions. Although youyuan declaration is mainly used in heavy-duty operators, in some cases, a namespace function, another member function or a complete class defined before this must be declared as a friend. When a class becomes a friend of another class, the member function of the friend class is granted the permission to access non-public members of the authorization class. Next we will learn more about functions rather than the friend declaration of operators. A class must declare every function in the overload function set with which it wants to establish a friend relationship as a friend. For example
123456789 externostream& storeOn( ostream &, Screen & );externBitMap& storeOn( BitMap &, Screen & );// ...classScreen{ friendostream& storeOn( ostream &, Screen & ); friendBitMap& storeOn( BitMap &, Screen & ); // ...};
If a function is used to manipulate two objects of different classes and the function needs to access non-public members of these two classes, the function can be declared as a friend of these two classes, or, as a member function of a class and declared as a friend of another class, let's take a look at how to do it: if we decide that a function must be declared as a friend of two classes, the membership declaration is as follows:
1234567891011 classWindow;// Declare onlyclassScreen{ friendboolis_equal( Screen &, Window & ); // ...};classWindow{ friendboolis_equal( Screen &, Window & ); // ...};
If we decide that a function must be a member function of a class and a friend of another class, the member function declaration and the membership Declaration are as follows:
classWindow;classScreen{public: // Copy is a member of the Screen class. Screen& copy( Window & ); // ...};classWindow{ // Copy is a friend of the Window class. friendScreen& Screen::copy( Window & ); // ...};
Only when the definition of a class has been seen can its member functions be declared as friends of another class. This is not always possible. For example, if the Screen class must declare the member functions of the Window class as friends, the Window class must declare the member functions of the Screen class as friends. What should I do? In this case, you can declare the entire Window class as a friend of the Screen class. For example:
123456 classWindow;classScreen{ friendclassWindow; // ...};
The non-public members of the Screen class can now be accessed by every member function of the Window.

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.