Overview:
1. Why do I need to use youyuan?
Generally, for common functions, it is impossible to protect members of the category class. To do so, you must make all the members of the class public (shared ), however, the problem is that any external function can access it without any constraints. The other method is to use the friend modifier of C ++, this allows some functions you set to operate on these private or protected data.
2. What are the disadvantages of using youyuan?
The use of youyuan also breaks the encapsulation feature of the class, which is the biggest drawback of youyuan. When you declare it as a friend, all your details are exposed to the other party.
It's as if you tell your friend that you have a wealth of money, and then tell him all about your money, antiques, family property, and other tips.
3. What do you think of youyuan?
Set a friend function or a friend class to tell the other party that all my elements are open to you. This kind of friend is based on mutual trust.
I. A normal function is a friend function of the class.
Declare a common function in the class and add the friend modifier before it. Then, this function becomes a friend of the class.
In this case, this common function can access all the members of this class.
The Code is as follows:
[Cpp] # include <iostream>
Using namespace std;
Class MyClass
{
Public:
MyClass (string name)
{
M_name = name;
}
// Declare a friend Function
Friend void Display (MyClass & mycalss );
Protected:
String m_name;
};
// Define this friend Function
// Enter void MyClass: Display (MyClass & mycalss)
Void Display (MyClass & mycalss)
{
Cout <"Access Protected data:" <mycalss. m_name <endl;
}
// Test
Int main (int argc, char * argv [])
{
MyClass test ("Class ");
Display (test );
Return 0;
}
# Include <iostream>
Using namespace std;
Class MyClass
{
Public:
MyClass (string name)
{
M_name = name;
}
// Declare a friend Function
Friend void Display (MyClass & mycalss );
Protected:
String m_name;
};
// Define this friend Function
// Enter void MyClass: Display (MyClass & mycalss)
Void Display (MyClass & mycalss)
{
Cout <"Access Protected data:" <mycalss. m_name <endl;
}
// Test
Int main (int argc, char * argv [])
{
MyClass test ("Class ");
Display (test );
Return 0;
} Note:
1. declare that this metafunction can be anywhere, either in public or protected, or in privated.
2. In this metafunction, you can access all the Members and member functions in this class, regardless of whether it is public, protected, or privated.
3. When defining a friend function, you cannot write it as void MyClass: Display (MyClass & mycalss.
2. A common function can be a friend function of multiple classes
Each class has a membership function declaration, which can have multiple declarations, but can only have one definition.
The Code is as follows:
[Cpp] # include <iostream>
Using namespace std;
Class MyClass_ B;
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
// Declare a friend Function
Friend void Display (MyClass_A & myA, MyClass_ B & myB );
Private:
String m_name;
};
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
// Note that another friend function is declared.
Friend void Display (MyClass_A & myA, MyClass_ B & myB );
Private:
String m_name;
};
// Define this friend Function
Void Display (MyClass_A & myA, MyClass_ B & myB)
{
Cout <"MyClass A:" <myA. m_name <endl;
Cout <"MyClass B:" <myB. m_name <endl;
}
// Test code
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class ");
Display (testA, testB );
Return 0;
}
# Include <iostream>
Using namespace std;
Class MyClass_ B;
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
// Declare a friend Function
Friend void Display (MyClass_A & myA, MyClass_ B & myB );
Private:
String m_name;
};
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
// Note that another friend function is declared.
Friend void Display (MyClass_A & myA, MyClass_ B & myB );
Private:
String m_name;
};
// Define this friend Function
Void Display (MyClass_A & myA, MyClass_ B & myB)
{
Cout <"MyClass A:" <myA. m_name <endl;
Cout <"MyClass B:" <myB. m_name <endl;
}
// Test code www.2cto.com
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class ");
Display (testA, testB );
Return 0;
}
Similarly, this metafunction can access all elements of these two classes.
3. A member function of a class can also be a friend of another class.
This allows a member function of a class to operate on data members of another class.
[Cpp] # include <iostream>
Using namespace std;
Class MyClass_ B;
// Class
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
Void Function (MyClass_ B & myB );
Private:
String m_name;
};
// Class B
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
// Youyuan function declaration. Note the difference between them and common functions.
Friend void MyClass_A: Function (MyClass_ B & myB );
Private:
String m_name;
};
// Function Definition
Void MyClass_A: Function (MyClass_ B & myB)
{
Cout <myB. m_name <endl;
}
// Test code
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class B ");
TestA. Function (testB );
Return 0;
}
# Include <iostream>
Using namespace std;
Class MyClass_ B;
// Class
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
Void Function (MyClass_ B & myB );
Private:
String m_name;
};
// Class B
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
// Youyuan function declaration. Note the difference between them and common functions.
Friend void MyClass_A: Function (MyClass_ B & myB );
Private:
String m_name;
};
// Function Definition
Void MyClass_A: Function (MyClass_ B & myB)
{
Cout <myB. m_name <endl;
}
// Test code
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class B ");
TestA. Function (testB );
Return 0;
}
We can see that Class B is open to A function in Class A, and the result is that this function can access all elements in Class B.
4. The entire class can also be a friend of another class.
Each member function of a friend class can access all the members of another class.
The sample code is as follows:
[Cpp] # include <iostream>
Using namespace std;
// Class
Class MyClass_ B;
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
// Youyuan Declaration
Friend class MyClass_ B;
Private:
String m_name;
};
// Class B
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
Void Display (MyClass_A & myA );
Private:
String m_name;
};
// Member functions
Void MyClass_ B: Display (MyClass_A & myA)
{
Cout <myA. m_name <endl; // access A's private member
MyClass_A test ("test ");
Cout <test. m_name <endl; // it seems that all elements of A exist in B.
}
// Test code
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class B ");
TestB. Display (testA );
Return 0;
}
# Include <iostream>
Using namespace std;
// Class
Class MyClass_ B;
Class MyClass_A
{
Public:
MyClass_A (string name)
{
M_name = name;
}
// Youyuan Declaration
Friend class MyClass_ B;
Private:
String m_name;
};
// Class B
Class MyClass_ B
{
Public:
MyClass_ B (string name)
{
M_name = name;
}
Void Display (MyClass_A & myA );
Private:
String m_name;
};
// Member functions
Void MyClass_ B: Display (MyClass_A & myA)
{
Cout <myA. m_name <endl; // access A's private member
MyClass_A test ("test ");
Cout <test. m_name <endl; // it seems that all elements of A exist in B.
}
// Test code
Int main (int argc, char * argv [])
{
MyClass_A testA ("Class ");
MyClass_ B testB ("Class B ");
TestB. Display (testA );
Return 0;
} In this case, B can access all the elements of A, just as A is in B.
V. Summary
Simply put, declaring a friend function or a friend class means completely exposing yourself to the other party.
Author lwbeyond