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

前端 未结 9 2190
南笙
南笙 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: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.

提交回复
热议问题