Classes and Objects (16)-Friends

Source: Internet
Author: User
Tags gety

1, friend Yuan

  after adopting the mechanism of the class, 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 public, and the communication interface between the class and the outside world is provided. However, there are times when you need to define functions that are not part of a class, but that require frequent access to the data members of the class, you can define these functions as friend functions for that function. In addition to the friend function, there are friend classes, which are collectively referred to as friends. The role of friends is to improve the efficiency of the program's operation (that is, reducing the type checking and security checks and so on requires time overhead), but it destroys the encapsulation and concealment of the class, so that non-member functions can 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.

2, 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 the class, it does not belong to any class, but it needs to be declared in the definition of the class, simply by adding a keyword friendto the name of the friend, in the following format:

friend type function name (formal parameter);

A function can be a friend function of more than one class, and it needs to be declared separately in each class.

Class member functions as friend functions:

Primitive methods (accessing members within a class by using getter):

#include <iostream>using namespacestd;classpoint{ Public: Point (intXinty) { This->x =x;  This->y =y; }    int GetX () {return  This-x; }    int GetY () {return  This-y; }Private:    intx; inty;}; //global function DoublePointdistance (Point &p1, Point &p2) {     return sqrt ((P1.getx ()-P2.GETX ()) * (P1.getx ()-P2.getx ()) + (P1.gety ()-p2.gety ()) * (P1.gety ()- p2.gety ()));}intMainvoid) {point P1 (1,2); Point P2 (2,2); cout<< pointdistance (p1, p2) <<Endl; return 0;}

Access the members of the class by using the friend function :

#include <iostream>using namespacestd;classpoint{ Public:    //declaring a global function pointdistance is a friend function of my class point class.    friend Double pointdistance (Point &p1, point & p2); Point (intXinty) { This->x =x;  This->y =y; }Private:    intx; inty;};DoublePointdistance (Point &p1, Point &p2) {     return sqrt ((p1.x-p2.x)) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y- p2.y));}intMainvoid) {point P1 (1,2); Point P2 (2,2); cout<< pointdistance (p1, p2) <<Endl; return 0;}

the selfless division between the same objects , Friends between heterogeneous objects

Class member functions as friend functions:

#include <iostream>using namespacestd;classPoint;//A forward declaration is an incomplete declaration that simply provides the class name (without providing a class implementation). Can only be used to declare pointers and references. classpointmanager{ Public:    DoublePointdistance (Point &p1, Point &p2);};classpoint{ Public:    //declaring a global function pointdistance is a friend function of my class point class. //friend Double pointdistance (Point &p1, point &p2);FriendDoublePointmanager::P ointdistance (Point &p1, Point &p2); Point (intXinty) { This->x =x;  This->y =y; }Private:    intx; inty;};DoublePointmanager::P ointdistance (Point &p1, Point &p2) {    returnsqrt ((p1.x-p2.x) * (p1.x-p2.x) + (P1.Y-P2.Y) * (P1.Y-p2.y));}intMainvoid) {point P1 (1,2); Point P2 (2,2); //cout << pointdistance (p1, p2) << Endl;Pointmanager pm; cout<< pm. Pointdistance (P1, p2) <<Endl; return 0;}
3. 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:

After the above instructions, all member functions of class B are friend functions of Class A, which can access the private and protected members of Class A.

4. On friend Yuan

Declaration Location

A friend declaration begins with a keyword friend, and it 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.

The pros and cons of friends

A friend is not a class member, 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. However, the access rights of classes are somewhat inflexible in some applications, thus tolerating the special grammatical phenomenon of friends.

Precautions

(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, it also depends on whether there is a corresponding declaration in the class.

Classes and Objects (16)-Friends

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.