Overview:
1. Why to make Ufida yuan.
Usually for ordinary functions, it is impossible to access the protection members of a class, and if you want to do so, you have to make the members of the class public (shared), but the problem with this is that any external function can access it without constraint and manipulate it; another way is to use C + + friend Modifier, which allows you to manipulate the private or protected data by some of the functions you set.
2. What are the disadvantages of using friends? the use of friends also destroys the package characteristics of the class, which is the biggest disadvantage of friends. When the foreign statement is a friend, all of your details are exposed to each other.
It's like you tell your friends that you're rich, and then you have all the money, how many antiques, how many possessions, how many concubines and so on.
3. How to understand the friends.
Set a friend function, or a friend of the class, is to tell each other: All my elements are open to you. This kind of friend is based on the grey often gray often trust each other on the basis of.
A common function as a friend function of a class declares a normal function in the class, and adds a friend modifier to it, then the function becomes a friend of the class.
This normal function can then access all members of the class.
The code is as follows:
#include <iostream >
using namespace std;
Class MyClass
{public
:
MyClass (string name)
{
m_name = name;
}
Declares a friend void Display (MyClass &mycalss);
Protected:
string m_name;
Define this friend function
//write void MyClass::D isplay (MyClass &mycalss)
void Display (MyClass &mycalss)
{
cout << "Access Protected data:" << mycalss.m_name << Endl;
}
Test
int main (int argc,char* argv[])
{
MyClass Test ("Class A");
Display (test);
return 0;
}
Description:
1. Declare this friend function can be anywhere, can be public, protected of course also can be in privated.
2. In this friend function, you can access all members of this class, all member functions, regardless of whether it is public, protected, or privated.
3. When defining a friend function, you cannot write void MyClass::D isplay (MyClass &mycalss) to be aware of this.
two. A normal function can be a friend function of more than one class
has a declaration of a friend function in each class, a declaration can have multiple, but only one definition. The
code is as follows:
#include <iostream >
using namespace std;
Class Myclass_b;
Class Myclass_a
{public
:
myclass_a (string name)
{
m_name = name;
}
Declares a friends 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 you also declare a 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 A");
Myclass_b testb ("Class A");
Display (Testa, testb);
return 0;
}
Again, this friend function can access all the elements of these two classes.
three. A member function of a class can also be a friend of another class so that the member functions of a class can manipulate the data members of another class
#include <iostream >
using namespace std;
Class Myclass_b;
Type A class
myclass_a
{public
:
myclass_a (string name)
{
m_name = name;
}
void Function (Myclass_b &myb);
Private:
string m_name;
};
Type B class
myclass_b
{public
:
myclass_b (string name)
{
m_name = name;
}
Friend
void Myclass_a::function (Myclass_b &myb), the difference between a common function and a function statement;
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 A");
Myclass_b testb ("Class B");
Testa.function (TESTB);
return 0;
}
We can see that Class B is open to one of the functions of Class A, and the result is that this function can access all the elements of Class B.
Four. The entire class can also be a friend of another class
Each member function of a friend class can access all members of another class.
The sample code is as follows:
#include <iostream >
using namespace std;
Class-A class
myclass_b;
Class Myclass_a
{public
:
myclass_a (string name)
{
m_name = name;
}
The friend class declares
friend classes Myclass_b;
Private:
string m_name;
};
Classes B class
Myclass_b
{public
:
myclass_b (string name)
{
m_name = name;
}
void Display (Myclass_a &mya);
Private:
string m_name;
};
member function
void Myclass_b::D isplay (myclass_a &mya)
{
cout<<mya.m_name<<endl;// Access the private member of a
myclass_a test ("test");
cout<<test.m_name<<endl; As if all of a's elements existed in B.
//test Code
int main (int argc,char* argv[])
{
myclass_a testa ("Class A");
Myclass_b testb ("Class B");
Testb.display (testa);
return 0;
}
At this point, B can access all the elements of a, as if a is inside B.
Five. Summary
Simply put: Declare a friend function or a friend class, is to expose themselves completely to each other .