Home>

A class can have members of three attributes:public, protected, and private.Objects can access public members, and only functions in this class can access private members of this class. Now, let's add an exception-friend.

Friend function

Functions that are defined outside the current class and do not belong to the current class can also be declared in the class.But the friend keyword is added in front of it, which constitutes a friend function.A friend function can be a non-member function that does not belong to any class.It can also be a member function of another class.

A friend function can access all members in the current class,Including private properties.

1) Declare ordinary functions as friend functions.

#include<iostream>
using namespace std;
class student {
private:
 char * name;
 int age;
 float score;
public:
 student (char *, int, float);
 friend void display (student);//declare display as a friend function
};
student ::student (char * name, int age, float score) {
 this->name=name;
 this->age=age;
 this->score=score;
}
//Ordinary member function
void display (student&stu) {
 The age of cout<stu.name<""
}
int main () {
 student stu ("Xiao Ming", 16, 95.5f);
 display (stu);
 return 0;
}

operation result:

Xiao Ming's age is 16 and his grade is 95.5

Please note that display is a function defined outside the class and is not qualified with student.It is a non-member function,Does not belong to any class,Its role is to output student information.If the display function is not declared as a friend function in the student class, it cannot reference the private members name, age, score in student. You can test it yourself,Delete line 11 in the above program and observe the information at compile time.

Now that display is declared as a friend function of student class, display can use the private members name, age, score in student. But note that you must add the object name when using these member variables,Cannot be written as:

cout<name<"'s age is "<age<"and the grade is"<score<endl;

Because display is not a member function of the student class,Members of the student class cannot be used by default,You must specify the object to be accessed.

2) Declare member functions of other classes as friend functions

The friend function can be not only a normal function (non-member function), but also a member function in another class.Consider the following example:

#include<iostream>
using namespace std;
class address;//Advance reference declaration for address class
//declare the student class
class student {
private:
 char * name;
 int age;
 float score;
public:
 student (char *, int, float);
 void display (address&);
};
//Declaration of the address class
class address {
private:
 char * province;
 char * city;
 char * district;
public:
 address (char *, char *, char *);
 //Declaring the member function display in the student class as a friend function
 friend void student ::display (address);
};
address ::address (char * province, char * city, char * district) {
 this->province=province;
 this->city=city;
 this->district=district;
}
//Declaring the student class as a constructor and member function
student ::student (char * name, int age, float score) {
 this->name=name;
 this->age=age;
 this->score=score;
}
void student ::display (address & add) {
 cout<name<"'s age is "<age<"and the grade is"<score<endl;
 cout<"Home Address:"<add.province<""Province"<add.city""""<endl;
}
int main () {
 student stu ("Xiao Ming", 16, 95.5f);
 address add ("Shaanxi", "Xi'an", "Yan Tower");
 stu.display (add);
 return 0;
}

operation result:

Xiao Ming's age is 16 and his grade is 95.5
Home Address:Yanta District, Xi'an, Shaanxi Province

In this example, two classes student and address are defined. Line 26 of the program declares the member function display in the student class as a friend function.From this, display can access private member variables of the address class.

Two points to note:

① The fourth line of the program makes an advance declaration for the address class.Because it was used in the student class before the address class was defined,If not stated in advance,Compilation will give an error,Prompt "address" has not been declared. The advance declaration of classes and the advance declaration of functions are the same.

② The declaration and definition of the student class are separated in the program.With address in the middle,This is because the member of the address class is used in the student ::display () function body,Must appear after the class body of the address class (the class body indicates which members are there).

Here is a brief introduction of the class's advance declaration.In general,Classes must be formally declared before they can be used;But in some cases (as shown in the example above), as long as you make a declaration in advance,You can also use it first.

It should be noted,The scope of advance declaration of a class is limited.You can use it to create objects only after you have officially declared a class.If you add a line after the fourth line of the above program:

address obj;//Attempt to define an object

An error occurs during compilation.Because when you create an object, you allocate memory for the object.Before the class is officially declared,The compilation system cannot determine how much space should be allocated for the object.The compiler can only determine how much space should be reserved for the object after "seeing" the class body (actually, seeing the member variables).After making a forward reference declaration for a class,You can use the name of the class to define a pointer variable or an object reference variable (such as in this example,Defines a reference variable of the address class object). This is because the sizes of pointer variables and reference variables themselves are fixed,It has nothing to do with the size of the class object it points to.

Note that the program officially declares the address class before defining the student ::display () function. This is because in the student ::display () function body, the member variables province, city, and district of the address class are used. If the address class is not formally declared, the compiler cannot recognize these member variables.

③ A function can be declared as a "friend" by multiple classes, so that it can reference private members in multiple classes.

Friend class

Not only can a function be declared as a "friend" of one class, but an entire class (such as class b) can also be declared as a "friend" of another class (such as class a). At this time, class b is a friend class of class a.

All functions in friend class b are friend functions of class a.Have access to all members in class a.Declare class b as a friend class with the following statement in the class body of class a:

friend b;

The general form of declaring a friend class is:

friend class name;

About FriendsTwo things need to be explained:

Friendship relationships are one-way, not two-way.If class b is declared as a friend of class a,Does not mean that class a is a friend class of class b,Member functions in class a cannot access private data in class b.

Friendship relationships cannot be passed on,If class b is a friend class of class a,Class c is a friend class of class b,Does not mean that class c is a friend class of class a.

In actual development,Unless it is really necessary,Generally do not declare the entire class as a friend class,And only declare member functions that really need them as friend functions,This is more secure.

c
  • Previous In-depth analysis of static member functions in C ++ programming
  • Next jQuery simple method to set the text box enter event