embedded linux C + + language (v)-friend
design is the communication interface between the class and the outside The
A friend can be a function that is called a friend function.
First,
Friends between heterogeneous objects
The essence of friend is to let other members not belong to this class ( global functions, member functions of other classes ), which are members of a class and have properties of members of this class.
1.
friend function
A friend function is a non-member function that can directly access a private member of a class, is a normal function that is defined outside the class, does not belong to any class, but needs to be declared in the definition of the class, simply by adding the keyword before the name of the friend friend, whose format is as follows :
Friend type function name ( formal parameter );
a function can be a friend function of more than one class, simply declaring it separately in each class .
A, global function as friend function
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 << "(" << x << "," << y << ")" << Endl;
}
Double Distance (Point &a, point &b)
{
Double dx = a.x-b.x;
Double dy = a.y-b.y;
return sqrt (DX*DX + dy*dy);
}
int main (void)
{
Point P1 (3.0, 4.0), P2 (6.0, 8.0);
P1. Getxy ();
P2. Getxy ();
Double d = Distance (P1, p2);
cout << "Distance is" << D << Endl;
return 0;
}
B, class member functions as friend functions
Class point;//declares the point class to ensure that the definition of the Managerpoint class does not go wrong
Class Managerpoint
{//define the Managerpoint class first to ensure that the distance function is declared in the point class with no error in the bit friend function
Public
Double Distance (Point &a, point &b);//can only be declared, forward declaration point cannot establish object
};
Class Point
{
Public
Point (double xx, double yy)
{
x = XX;
y = yy;
}
void Getxy ();
Friend Double managerpoint::D istance (Point &a, point &b);
Private
Double x, y;
};
void Point::getxy ()
{
cout << "(" << x << "," << y << ")" << Endl;
}
Double managerpoint::D istance (Point &a, point &b)
The {//distance function must be defined after the point class definition to use the object of the point class
Double dx = a.x-b.x;
Double dy = a.y-b.y;
return sqrt (DX*DX + dy*dy);
}
int main (void)
{
Point P1 (3.0, 4.0), P2 (6.0, 8.0);
P1. Getxy ();
P2. Getxy ();
Managerpoint MP;
Float D = MP. Distance (P1,P2);
cout << "Distance is" << d<< Endl;
return 0;
}
forward declaration, is an incomplete type(forward Declaration) declares that you simply provide the class name ( without providing a class implementation ) . The Forward declaration function is limited:
A, You cannot define an object for a class.
B, can be used to define pointers or references to this type.
C, used to declare ( not defined ) Use this type as a function of a formal parameter or return type.
2.
Friend Object
All member functions of a friend class are friend functions of another class and can access hidden information in another class, including private members and protected members. When you want a class to be able to access the private members of another class, you can declare the class as a friend of another class. The statement format that defines the friend class is as follows:
Friend class name ;
Friend and class are keywords, and the class name must be a class that has already been defined in the program.
For example, the following statement describes the class B is a class A Friend class:
Class A
{
...
Public
Friend class B;
...
};
All member functions of class B are friend functions of class a that can access Private and protected members of Class A.
Second,
Use of Friends
Friend declaration with keyword friend starts, and can only appear in the class definition. Because the friend is not a member of the authorization class, it is not affected by the public,Private, and protected of the declaration area of the class in which it resides . Usually we choose to group all the friend declarations together and put them behind the class header .
A friend is not a class member, but it can access a private member in a class. The function of friend is to improve the running efficiency of the program, but it destroys the encapsulation and concealment of the class, so that the non-member function can access the private members of the class.
Features of friend:
A, A friend relationship cannot be inherited.
B, A friend relationship is one-way and does not have a commutative nature. If the class b is a friend of class a , class a is not necessarily a friend of class B , To see if there is a corresponding declaration in the class.
C, friend relationships do not have transitivity. If the class b is a friend of class A , class C is a friend of b , class c Not necessarily a class A friends, also to see whether there is a corresponding declaration in the class .
This article from "Endless life, Struggle not only" blog, reprint please contact the author!
Embedded Linux C + + language (v)-friend