The following is a detailed analysis of the four operators in C ++ that cannot be overloaded as a friend function. For more information, see
C ++ requires four operators: =,->, [], and (). It cannot be an overload in the local area (that is, it cannot be a member function). Why?
Now let's talk about the overload of the value assignment operator "= ".
C ++ requires that the value assignment operator "=" can only be a non-static member function of the class, rather than a friend function of the class.
It is easier to understand static members that cannot be reloaded as classes, because static member functions belong to the entire class and do not belong to an object. They can only operate static data members of the class. The value assignment operator "=" is based on object operations.
So why can't the value assignment operator be reloaded as a class membership function? +, Which is also a binary operator. Why is it okay?
Before discussing this issue, let's first look at the test program:
Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;
Class
{
Private:
Int x;
Public:
A () {x = 99 ;}
A (int xx)
{
Cout <"Call A (int xx)" <endl;
X = xx;
}
};
Int main ()
{
A;
A = 7;
}
The program execution result is:
Call A (int xx)
It indicates that when the Program Statement a = 7 is executed, the program calls the constructor with parameters in Class.
Add A value assignment operation to Class A to overload the member function, as shown below:
Copy codeThe Code is as follows:
# Include <iostream>
Using namespace std;
Class
{
Private:
Int x;
Public:
A () {x = 99 ;}
A (int xx)
{
Cout <"Call A (int xx)" <endl;
X = xx;
}
A operator = (int xx) // overload value assignment operator
{
Cout <"Call A operator = (int xx)" <endl;
X = xx;
Return * this;
}
};
Int main ()
{
A;
A = 7;
}
Program running result:
Call A operator = (int xx)
This indicates that when the corresponding value assignment operator in Class A reloads a function, the program will call the corresponding value assignment operator in Class A to reload the function, instead of calling the constructor with parameters as previously described above.
Here, we can make the following judgment on the C ++ rule:
When no value assignment operator is defined in the class to reload member functions (note that the compiler will automatically generate and add the function when the value assignment operator of the Data Type of the parameter is not defined to overload the function ), when the program executes a value assignment sentence, the program calls the constructor that matches the right value type in the value assignment statement, and regards the right value as the real parameter of the constructor. For example, when the initial value assignment statement a = 7 is executed, the actual operation is a (7 ). When there is a defined value assignment operator in the class to reload the member function and execute the value assignment statement, the program will only call the corresponding value assignment operator to reload the function.
After understanding the above rules, we can now come back and discuss why the value assignment operator cannot be reloaded as a friend function of the class.
We know that a friend function is not a member function of the class. It is only a friend of the class and has the permission to access data members of the class that declares it as a friend.
When the value assignment operator is overloaded as a member function of the class and the assignment statement of the Class Object is executed in the program, the program will have two conflicting options.
1. Because the class does not have a member function that overload the value assignment operator, it calls the corresponding constructor according to the C ++ rules.
2. But in the global environment, we have already overloaded the value assignment operator function of the parameter type for this class type, and the value assignment statement exactly matches this function. According to the C ++ rule, this function will also be called.
The program does not allow conflicting and uncertain choices. Therefore, when the value assignment operator is overloaded as a friend function of the class, the compiler will prompt an error.
For the remaining three operators->, [], (), why can't we reload them as friend functions? That's the same as above. That is, the compiler finds that when the class does not define the overload member functions of the three operators, it will add its own default operators to the overload member functions.
For example, if Class A does not have an overload member function that defines the operator>, we can still call the member in the object pointed to by the pointer to Class A in the form of->. For example, Class A has A member function f (). When
Copy codeThe Code is as follows:
A;
A * p = &;
P-> f (); // although class A does not have its own defined operator-> overload member functions, it can still be used here
However, when we reload the-> operator as A friend function of Class A, the program will be the same as the value assignment operator to overload friends, that is, there is A conflict.
Statement: The above are only personal opinions.