C + + friend function and friend class

Source: Internet
Author: User
Tags function definition

1. The need for friend functions and friend classes:
The class has encapsulation and information hiding attributes.

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 public, this destroys the hidden attributes.

In addition, it should be seen that in some cases, especially when multiple calls are made to certain member functions, the time cost is required for parameter passing, type checking and security checking, which affects the efficiency of the program.
  

2. In order to solve the above problems, a scheme is proposed to make the UF element.

A friend is a normal function that is defined outside the class, but it needs to be described in the class, and in order to distinguish it from the member functions of the class, the keyword friend is preceded by the description.

A friend is not a member function, but it can access a private member in a class. The role of friends is to improve the efficiency of the program, but it destroys the encapsulation and concealment of the class, allowing non-member functions to access the private members of the class.

A friend can be a function that is 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 non-member function of a private member of a class. The friend function, syntactically, is the same as a normal function, which is the same as a normal function on a definition and a call.

The following example illustrates the application of a friend function.

#include "iostream" #include "Cmath" using namespace Std;class point{private:double x,y;public:point (double xx, double yy ) {x=xx; y=yy;} void Getxy (); friend Double Distance (Point &a, point &b);  Friend identifies it as a friend function, not a member function,};void point::getxy () {cout<< "(" <<x<< "," <<y<< ")" << Endl;} Double Distance (Point &a, point &b) {double dx = a.x-b.x;  Private members in a class can be accessed 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);p 1. Getxy ();    Call the member function P2. Getxy ();d ouble d = Distance (P1, p2);  When you call the meta-function, you do not call the cout<< "The Distance is" <<d<<endl;system ("pause") as a member function, as is called in the normal function; return 0;}

Description

A friend function distance () is described in the Point class in the program, which adds the friend keyword in front of the description to identify that it is not a member function, but a friend function.

It is defined in the same way as a normal function definition, and differs from the definition of a member function because it does not need to indicate the class to which it belongs. However, it can refer to private members in a class, where a.x,b.x,a.y,b.y are private members of the class that are referenced by objects.

When you call the meta-function, it is the same as calling the normal function, 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. While distance (P1, p2) is a friend function call, it is called directly, does not need an object representation, its argument is an object.

Friends: Friends In addition to the previous function, 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 all member functions of the class are friend functions of the other class. 

3. Note When using the friend class:

(1) A friend relationship cannot be inherited.   

(2) Friend relationship is one-way and not commutative. If Class B is a friend of Class A, class A is not necessarily a friend of Class B, it depends on whether there is a corresponding declaration in the class. 

(3) Friend relationship does not have transitive nature. 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, but also to see if there is a corresponding declaration in the class   

4. Sum up:

(1) A friend relationship cannot be inherited, but access rights do not change for an existing method.   

(2) If the method of overriding the base class changes the access permission 

(3) Friend relationship does not have transitivity, 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

Reference: http://blog.csdn.net/hackbuteer1/article/details/6568369

C + + friend function and friend class

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.