C++ — How to overload operator+=?

前端 未结 6 2084
北海茫月
北海茫月 2020-12-05 23:13

Given the following code snippet,

class Num
{
public:
    Num(int iNumber = 0) : m_iNumber(iNumber) {}

    Num operator+=(const Num& rhs)
    {
                 


        
相关标签:
6条回答
  • 2020-12-05 23:18

    Write the body like you did. Use the operator like you did. The operator should return a reference to allow chaining.

    See here.

    0 讨论(0)
  • 2020-12-05 23:27

    Returning by reference would be better

    Num& operator+=(const Num& rhs){
    
          this->m_iNumber += rhs.m_iNumber;
          return *this;
    }
    
    0 讨论(0)
  • 2020-12-05 23:37

    The signature of any of the assignment operators (operator= or operator @= for your favorite operator @) should be

    Class& operator @= (const Class& rhs);
    

    That is, the function takes its parameter by const reference (because it doesn't modify it), then returns a mutable reference to the object. The reason you return a non-const reference is because, for historical reasons, you can write code like this:

    (a += b) += c;
    

    This is by no means good style, but it works for ints and so you should endeavor to make it work for your types as well.

    As for the body of your function, what you have there is perfectly correct. In general, though, you should be sure that the code works even if the parameter is the receiver object. For example, if you write something like

    a += a;
    

    This gets translated into

    a.operator+= (a);
    

    And so the code will operate with both the parameter and the receiver being the same object. This usually doesn't come up for compound assignment operators (usually only operator= needs to worry about this), but in some cases you can get burned if you're not careful.

    Finally, you can use this operator += function just as you have been in the example code above. Any use of += automatically calls it.

    Hope this helps!

    0 讨论(0)
  • 2020-12-05 23:39

    Your example is completely correct: http://codepad.org/PVhQw9sc .

    1. You can define the return value to be whatever you want. If you wanted it to match what int does, it would need to return the type Num&, and return the value *this.
    2. You did it correctly.
    3. You also did that correctly.
    0 讨论(0)
  • 2020-12-05 23:42

    If you follow to this Wikibook you'll find these answers by example:

    1. Type& operator+=(const Type& right) is the correct signature.
    2. You did it right.
    3. You use an overloaded operator just like the plain operators, only with your type in place. That's kind of the point of the exercise. ;)

    Note as an added point (which you did right) the compound assignment operators have to be member functions.

    0 讨论(0)
  • 2020-12-05 23:43

    Your operator function could also be written as

    Num& operator += (const Num& rhs)
    {
       m_iNumber += rhs.m_iNumber;
       return m_iNumber;
    }
    

    *this->m_iNumber and m_iNumber by itself within a member function are the same and using the former format is more typing in my opinion.

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