Friends functions and friends Functions

Source: Internet
Author: User

Today, I have nothing to worry about. I have reviewed the membership functions and membership classes of C ++. Here, I will record my experiences and deepen my impression.

First, we need to know what a friend functions are. The so-called friend functions have to be a function (nonsense ). Secondly, this function must be explicitly declared as a friend function by a class.

Example: 1 Class

2 {

3 friend void setx (A &, INT); // declare setx as a friend Function

4

5 public:

6 A (): x (0) {} // initialize X to 0

7 private:

8 int X;

9 };

10

11 void setx (A & A, int B)

12 {

13 A. X = B;

14}

15

16 int main ()

17 {

18;

19 int B = 10;

20 setx (A, B );

21 return 0;

22}

The whole program is very simple. first create a class A, then create a value assignment function setx, and then execute the operation to assign values to the private variable X of A in main, and then return 0 to end the program. Class A has a common constructor and a private member variable X. In row 3rd, the setx (A &, INT) function is also declared. The method for declaring the function is to add the friend keyword before the return value.

The setx function is implemented outside the class, and the function is very simple. It is used to assign values to the private member variable X of object. we know that private members of a class can only access their internal functions and cannot directly access them outside the class, But setx has been declared as a friend function by Class, the setx function can directly access its private variable X through object.

The sub-function is used to use private member variables or methods of a class externally. Youyuan class and youyuan function also mean that you only need to declare a class in Class A as its youyuan class. In this way, you can call the private variables and methods in Class.

For example, James has an airplane model, which is private to James. Only James can play on his own, and others cannot play. At this time, James is equivalent to Class A, and the model is equivalent to the private variable X. Maybe James thinks Xiaogang is his good friend, so Xiaogang can play with his airplane model. The code is like this:

Class James

{

Friend Xiaogang;

PRIVATE:

Play ("airplane model"); // Private Method

};

Class Xiaogang

{

Play ()

{

James. Play ("airplane model ");

}

};

However, youyuan is not a two-way solution, but a one-way solution. Although James thinks that Xiaogang is a friend and is willing to share his toys with Xiaogang, Xiaogang does not think so, in his mind, James may not be a friend, so when James wants to play with Xiaogang's toys, he may be rejected by Xiaogang, or even beaten by Xiaogang. (I have seen many children share their toys with others)

So this is just like youyuan. You are wishful thinking. You treat others as friends, but others take you as a pot dish. So be cautious when making friends! Pai_^

Friends functions and friends Functions

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.