Has anyone found the need to declare the return parameter of a copy assignment operator const?

前端 未结 7 2053
误落风尘
误落风尘 2020-12-14 12:02

The copy assignment operator has the usual signature:

    my_class & operator = (my_class const & rhs);

Does the following signatur

相关标签:
7条回答
  • 2020-12-14 13:07

    Yes, it should be const. Otherwise clients can do this:

    class MyClass
    {
    public:
          MyClass & operator = (MyClass const & rhs);
    }
    
    void Foo() {
        MyClass a, b, c;
        (a = b) = c; //Yikes!
    }
    
    0 讨论(0)
提交回复
热议问题