How can a returned object be assignable?

前端 未结 1 1517
我寻月下人不归
我寻月下人不归 2020-12-14 18:52

In Effective C++, Item 3, Scott Meyers suggests overloading operator* for a class named Rational:

    class Rational { ... };
    c         


        
相关标签:
1条回答
  • 2020-12-14 19:11

    The point is that for class types, a = b is just a shorthand to a.operator=(b), where operator= is a member function. And member functions can be called on rvalues.

    Note that in C++11 you can inhibit that by making operator= lvalue-only:

    class Rational
    {
    public:
      Rational& operator=(Rational const& other) &;
      // ...
    };
    

    The & tells the compiler that this function may not be called on rvalues.

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