english/basic_content/friend/README.md
Friends provide a mechanism for ordinary functions or class member functions to access private or protected members in another class.In other words, there are two forms of friends:
(1)Friend Function:Ordinary functions access a private or protected member of a class.
(2)Friend Class:Member functions in class a access private or protected members in class B
Advantages: improve the efficiency of the program.
Disadvantages: it destroys the encapsulation of classes and the transparency of data. Conclusion:
It is declared in any region of the class declaration, and the definition is outside the class.
friend <type><name>(<Parameter table>);
Note that the friend function is only a common function, not a class member function of this class. It can be called anywhere. In the friend function, private or protected members of the class can be accessed through the object name.
Code:friend_func.cpp
#include <iostream>
using namespace std;
class A
{
public:
A(int _a):a(_a){};
friend int geta(A &ca); ///< Friend function
private:
int a;
};
int geta(A &ca)
{
return ca.a;
}
int main()
{
A a(3);
cout<<geta(a)<<endl;
return 0;
}
The declaration of a friend class is in the declaration of the class, and the implementation is outside the class.
friend class <friend class name>;
Class B is a friend of class A, so class B can directly access private members of A. Code:friend_class.cpp
#include <iostream>
using namespace std;
class A
{
public:
A(int _a):a(_a){};
friend class B;
private:
int a;
};
class B
{
public:
int getb(A ca) {
return ca.a;
};
};
int main()
{
A a(3);
B b;
cout<<b.getb(a)<<endl;
return 0;
}