C + + keyword--friend

Source: Internet
Author: User

Friend means:

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, sometimes you need to define functions that are not part of the class (note that the friend function is not part of the class), but you need to access the data members of the class frequently, and 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.

classData{public:...friend  int set(int&m);//友元函数friend  class peop;     //友元类...}

Friend is divided into friend function and friend class, two kinds have different invocation forms:

A 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 defined outside the class, it does not belong to any class, but it needs to be in the definition of the class

Declaration, simply add the keyword friend in front of the friend's name, in the following format:

Friend type function name (formal parameter);

1. The declaration of a friend function can be placed in the private part of the class or in the public part, they are no different, it is a friend function of the class.


2. A function can be a friend function of more than one class, and it needs to be declared separately in each class. The call of the friend function is consistent with the method and principle of calling the general function.

B Friend class

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

When a class can access a private member of another class, the class can be declared as a friend of another class.


For example, the following statement illustrates that Class B is a friend class of Class A:

Class a{
...
Public
Friend class B;
...
};

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.

Note When using friend classes:

(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) A friend function is not a member function of a class, so class::function name cannot be added when defined outside the class

Let's look at an example of a friend in full:

From friends--Interactive Encyclopedia
#include #includeclassPoint//Statement{ Public: Point (DoubleXxDoubleyy) {x=xx; y=yy;}//default constructor      voidGetxy ();//Public member functionsFriendDoubleDistance (Point &a, point &b);//friend function    Private:Doublex, y;  }; voidPoint::getxy () {cout<<"("<}DoubleDistance (Point &a, point &b)//note the non-class declarator before the function name{DoubleDX = a.x-b.x; DoubleDY = a.y-b.y; returnsqrt (dx*dx+dy*dy); }voidMain () {point P1 (3.0,4.0), p2 (6.0,8.0); P1.  Getxy (); P2.  Getxy (); DoubleD =Distance (P1, p2); cout<<"Distance is"<}

C + + keyword--friend

Related Article

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.