Why cannot a non-member function be used for overloading the assignment operator?

前端 未结 9 2143
南笙
南笙 2020-11-27 18:51

The assignment operator can be overloaded using a member function but not a non-member friend function:

class Test
{
    int a;
public:
    Test         


        
相关标签:
9条回答
  • 2020-11-27 19:38

    Because there is already an implicit operator overloading function for '=' in the class to do shallow copying. So even if you overload using a a friend function you will never be able to call it as any call made by us would call the implicit shallow copying method rather than the overloaded friend function.

    0 讨论(0)
  • 2020-11-27 19:45

    Because the default operator= provided by the compiler (the memberwise copy one) would always take precedence. I.e. your friend operator= would never be called.

    EDIT: This answer is answering the

    Whats the inherent problem/limitation in supporting = operator ?

    portion of the question. The other answers here quote the portion of the standard that says you can't do it, but this is most likely why that portion of the standard was written that way.

    0 讨论(0)
  • 2020-11-27 19:45

    Firstly, it should be noted that this has nothing to do with the operator being implemented as a friend specifically. It is really about implementing the copy-assignment as a member function or as a non-member (standalone) function. Whether that standalone function is going to be a friend or not is completely irrelevant: it might be, it might not be, depending on what it wants to access inside the class.

    Now, the answer to this question is given in D&E book (The Design and Evolution of C++). The reason for this is that the compiler always declares/defines a member copy-assignment operator for the class (if you don't declare your own member copy-assignment operator).

    If the language also allowed declaring copy-assignment operator as a standalone (non-member) function, you could end up with the following

    // Class definition
    class SomeClass {
      // No copy-assignment operator declared here
      // so the compiler declares its own implicitly
      ...
    };
    
    SomeClass a, b;
    
    void foo() {
      a = b;
      // The code here will use the compiler-declared copy-assignment for `SomeClass`
      // because it doesn't know anything about any other copy-assignment operators
    }
    
    // Your standalone assignment operator
    SomeClass& operator =(SomeClass& lhs, const SomeClass& rhs);
    
    void bar() {
      a = b;
      // The code here will use your standalone copy-assigment for `SomeClass`
      // and not the compiler-declared one 
    }
    

    As seen in the above example, the semantics of the copy-assignment would change in the middle of the translation unit - before the declaration of your standalone operator the compiler's version is used. After the declaration your version is used. The behavior of the program will change depending on where you put the declaration of your standalone copy-assignment operator.

    This was considered unacceptably dangerous (and it is), so C++ doesn't allow copy-assignment operator to be declared as a standalone function.

    It is true that in your particular example, which happens to use a friend function specifically, the operator is declared very early, inside the class definition (since that's how friends are declared). So, in your case the compiler will, of course, know about the existence of your operator right away. However, from the point of view of C++ language the general issue is not related to friend functions in any way. From the point of view of C++ language it is about member functions vs. non-member functions, and non-member overloading of copy-assignment is just prohibited entirely for the reasons described above.

    0 讨论(0)
提交回复
热议问题