Resolves four operators in C++ that cannot be overridden as friend functions

  • 2020-04-02 01:21:49
  • OfStack

C++ specifies four operators =, - > , [], () cannot be overridden in the whole region. Why?
Now let's talk about overloading the assignment operator "=.
C++ specifies that the assignment operator "=" can only be overridden as a non-static member function of a class, not as a friend function of a class.
Static members that cannot be overloaded as classes should be easier to understand, because static member functions belong to the class as a whole, not to an object, and can only manipulate static data members of a class. The assignment operator "=" is based on object manipulation.
So why can't the assignment operator be overridden as a friend of a class? Like +, which is the same binocular operator, why does it work?

Before we discuss this, let's take a look at the test procedure:


#include <iostream>
using namespace std;

class A
{
private: 
  int x;
public:
         A(){x=99;}
         A(int xx)
         {
                   cout<<"Call A(int xx)"<<endl;
                   x = xx;
         }
};
int main()
{
         A a;
         a = 7;
}

The execution result of the program is:
Call A xx (int)

When a = 7 is executed, the program calls the argument constructor in class a.
Add an assignment operation to class A and overload the member function as follows:


#include <iostream>
using namespace std;

class A
{
private:
         int x;
public:
         A(){x=99;}
         A(int xx)
         {
                   cout<<"Call A(int xx)"<<endl;
                   x = xx;
         }
         A operator=(int xx)   //Overloaded assignment operator operation
         {
                   cout<<"Call A operator=(int xx)"<<endl;
                   x = xx;
                   return *this;
         }
};

int main()
{
         A a;
         a = 7;
}

Program running results:
Call A operator = (int xx)

Indicates that the assignment statement A = 7 is executed when the corresponding assignment operator overload function is already in class A. Instead of calling the argument constructor, the program calls the corresponding assignment operator overload function in class A.

Here, we can make the following judgments about C++ rules:
When the class does not define the assignment operator overloading member functions (note that in the undefined shape parameters according to the type of the class type of the assignment operator overloading function, the compiler will automatically generate join), when the program execution to an assignment statement, the program is invoked with the assignment statement right value type matching constructor, and the right values as the constructor arguments. Like the original assignment statement a = 7, when executed, the actual action is a(7). When an assignment operator is defined in a class to override a member function and execute an assignment statement, the program will only call the corresponding assignment operator overload function.

Now that you understand the above rules, you can go back and discuss why the assignment operator cannot be overridden as a friend function of a class.

We know that the friend function is not a member of the class, it is just a "friend" of the class, with access to the data member of the class that declared it a "friend".
Then when the 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 contradictory choices.

1. Because it believes that there is no member function in the class that overloads the assignment operator, it calls the corresponding constructor according to the rules of C++.

2. But globally, we've overloaded the assignment operator function with an argument type of this type, and the assignment statement matches the function, and according to the rules of C++, it calls the function as well.

Programs are not allowed to have conflicting and uncertain choices, so when the assignment operator is overridden as a friend function of a class, the compiler prompts for an error.

For the remaining three operators - > , [], (), why can't be overridden as a friend function, the same reason as above. When the compiler finds that there is no overloaded member function that defines the three operators in the class, it adds the default operator overloaded member function.
When the operator - is not defined in class A > But we can still use - for class A object Pointers > Calls a member of the object to which the pointer is pointing. Class A has A member function f(), when


A a;
A* p = &a;
 p->f();   //Although there is no self defined operator in class A -> Overloaded member functions, but can still be used here

However, when we put - > When the operator is overloaded as A friend function of class A, the same situation occurs when the assignment operator overloads the friend.
Disclaimer: the above is only personal opinion


Related articles: