C + + in the friend detailed __c++

Source: Internet
Author: User
Tags class definition function definition

The question of the proposed

We already know that classes have the characteristics of encapsulation and information hiding. Only member functions of a class can access private members of a class, and other functions in the program cannot access private members. Non-member functions can access public members in a class, but if the data members are defined as common, this also destroys the hidden attributes. In addition, you should see that in some cases, especially when multiple calls are made to some member functions, because of parameter passing, type checking, security checking, and so on, it can affect the running efficiency of the program.

In order to solve the above problems, this paper puts forward a scheme to make ufida. A friend is a generic function that is defined outside the class, but he needs to be described in the class body, to distinguish it from the member functions of the class, and to use the keyword friend before the description. A friend is not a member function, but he can access private members in a class. The role of the friend is to improve the efficiency of the program, but he destroys the encapsulation and concealment of the class, allowing the Non-members function to access the private members of the class.

A friend can be a function called a friend function; A friend can also be a class, which is called a friend class.

Friend function

A friend function is characterized by the ability to access a member function of a private member in a class. The friend function is syntactically the same as a normal function, that is, on the definition and on the call, and on the normal function. The following example illustrates the application of a friend function.

#include
#include

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<< "(" <<< "," <<< ")" << font>
}

Double Distance (Point &a, point &b)
{
Double dx = a.x-b.x;
Double dy = a.y-b.y;
return sqrt (dx*dx+dy*dy);
}

void Main ()
{
Point P1 (3.0, 4.0), P2 (6.0, 8.0);
P1. Getxy ();
P2. Getxy ();
Double d = Distance (P1, p2);
cout<< "Distance is" <<< font>
}

Description: A friend function distance () is described in the Point class in the program, and he adds the friend keyword in front of the description to identify that he is not a member function, but a friend function. His definition method is the same as the normal function definition, but differs from the definition of the member function, because he does not need to indicate the class to which it belongs. However, he is able to refer to private members in the class, and a.x,b.x,a.y,b.y in the function body are private members of the class, and they are referenced through objects. When you are calling the function, it is the same as the normal function call, not as a member function. In this case, p1. Getxy () and P2. Getxy () This is a call to a member function, to be represented by an object. and distance (P1, p2) is a friend function call, which he calls directly, without object representations, his arguments are objects. (The program's function is to know two points of coordinates, to find two points of distance.) )

Friend class

Friends In addition to the previous mentioned functions, the friend can also be a class, that is, a class can be a friend of another class. When a class is a friend of another class, this means that any member function of the class is a friend function of another class.

---------------------------------------------------another article

After adopting the class mechanism, the data hiding and encapsulation are realized, the data members of the class are generally defined as private members, and the member functions are generally defined as common, which provides the communication interface between the class and the outside. However, there are times when you need to define functions that are not part of a class, but require frequent access to the data members of the class, and you can define these functions as friend functions for that function. In addition to the friend function, there are friends of the class, the two collectively referred to as friends. The role of the friend is to improve the efficiency of the program (that is, it takes time to reduce the type check and security checks, etc.), but it destroys the encapsulation and concealment of the class, allowing Non-members to access the private members of the class.
Friend function:
A friend function is a non-member function that can directly access a private member of a class. It is a normal function that is defined outside of a class, and it does not belong to any class, but it needs to be declared in the definition of a class by adding the keyword friend to the name of the friend, in the following format:
Friend type function name (formal parameter);

The declaration of a friend function can be placed in the private part of the class, or it can be placed in the public domain, and they are indistinguishable, all of which indicate a friend function of the class.
A function can be a friend function of more than one class, and it needs to be declared separately in each class.
The invocation of a friend function is consistent with the way and principle that a general function is invoked.

Friend class:
All member functions of a friend class are friend functions of another class, and you can access hidden information in another class, including private and protected members.
When you want a class to be able to access a private member of another class, you can declare the class as a friend of another category. The statement format that defines a friend class is as follows:
Friend class class name;
Where: friend and class are keywords, and class names must be a defined class in the program.

For example, the following statement shows that Class B is a friend of Class A:
Class A
{
...
Public
Friend class B;
...
};
After the above description, all member functions of Class B are friend functions of Class A, which can access private members of Class A and protect members.

Note When using the friend class:
(1) The friend relationship cannot be inherited.
(2) The friend relation is one-way, does not have the exchange sex. If 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.
(3) The friendship relationship is not transitive. If Class B is a friend of Class A, Class C is a friend of B, Class C is not necessarily a friend of Class A, it also depends on whether there is a corresponding declaration in class

Precautions:

1. Friends can access private members of a class.

2. Only within a class definition, a friend declaration can be anywhere in a class, usually at the beginning or end of a class definition.

3. A friend can be a normal, non-member function, or a member function of another class defined earlier, or an entire class.

4. The class must declare every function in the overloaded function set that you want to be a friend as a friend.

5. A friend relationship cannot be inherited, and a friend of the base class has no special access rights to the members of the derived class. If the base class is granted a friend relationship, only the base class has special access rights. A derived class of this base class cannot access a class that is granted a friend relationship.


Original post address: http://blog.chinaunix.net/uid-790245-id-2037327.html

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.