Excerpt from youyuan

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

 

1. Friend meta function: Friend ret-type classname: funname (....);
2. Friend Meta class: Friend class classname;
The only thing to note is that whether it is a friend function or a friend class, you must declare
Youyuan class is actually something to tell. Once the class inherits, The youyuan relationship is still a little complicated. The following is a simple description:

C declares that A is its friend Meta class, so the most basic thing is that a can use the private method or object in C.
As shown in the figure, a is the base class of B, and C is the base class of D. ABCD has the following relationships:
1. The newly added method bcannot access the private member of C.
2. B's method inherited from a can access the private member of C.
3. A can only access the Private Members inherited from C in D. The new private members in D cannot access it!
In summary, the relationship between friends and friends cannot be inherited, but the access permissions of existing methods remain unchanged. If the base class method is changed, the access permission is changed.
PS: Thank you very much for your comments and corrected my mistakes. This time I posted the test code together.
Class C
{
Friend Class;
PRIVATE:
Int numc;
};

Class D: Public C
{
PRIVATE:
Int numd;
};

Class
{
Public:
// A is a friend of C and can access the private member of C.
Void getfromc ()
{
C;
C. numc = 100;
Cout };
// A is not a friend of D. It cannot access the newly added private member of D, but can access the private member inherited by D from C.
Void getfromd ()
{
D;
D. numc = 23;
Cout // D. numd = 24;
// Cout}
};

Class B: public
{
Public:
// The friend relationship cannot be inherited, and the new method cannot access the private member of C.
// Int getnumb ()
//{
// C;
// C. numc = 100;
// Return C. numc;
//}
// The access permission for C is changed after the method is changed. Private Members of C cannot be accessed.
// Void getfromc ()
//{
// C;
// C. numc = 100;
// Cout //}

};
Int _ tmain (INT argc, _ tchar * argv [])
{
A;
A. getfromc (); // output -- getfromc in a: numc = 100
A. getfromd (); // output -- getfromd in a: numc = 23
B;
B. getfromc (); // output -- getfromc IN A: numc = 100
// Call A: getfromc
Return 0;
}

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.