Alex
Alex

Reputation: 876

Returning member methods overloads by reference or not

So I saw a video talking about operator overloads in a class. This class was a fictitious Number class which instantiated real numbers and could be used like this Number first(1) or Number second(2) (numbers are stored in a num data member) and then we overloaded the operators as member methods , to be able to do this first + second or -first. The code for these operator overloads looked like this:

PS: rhs for right hand side

Number &Number::operator+(const Number &rhs) {
   //perform math calculation between both objects
   return *this;  //return lhs by reference
}

or this for the unary minus operator which just returns the minus version of a single integer (useless but used as an example):

Number operator-() {
   int temp = -num;
   return temp;//temporary
}

Now the video said that we should return an object by reference if it was used in a chain operations after it has gone throught the overload, I also heard it depends of the performance (is it to avoid copying?), but after that I'm still unsure when to return an object by reference or not in class operator overloads.

Thanks in advance.

Upvotes: 0

Views: 115

Answers (1)

mmcblk1
mmcblk1

Reputation: 168

You can either call the overloaded operator, as either:

  1. Binary Arithmetic Operation: Call an overloaded operator function directly in the same way an ordinary function is called:

    number1 + number2;              // normal expression
    operator+(number1, number2);    // equivalent function call
    

    In this case, both calls are equivalent. Both call the nonmember function operator+, passing number1 as the first argument and number2 as the second. The binary arithmetic operation doesn't modify either operand - it actually returns a new value from the two arguments. Hence you dont return a reference.

  2. Compound Assignment Operation: The other option is to call the member operator function explicitly:

    number1 += number2;             // expression-based call
    number1.operator+=(number2);    // equivalent call to a member operator function 
    

    Here, both the statements, call the member function operator+=, binding this to the address of number1 and passing number2 as an argument. Hence you return it as a reference.

    Number& operator+=(const Number& rhs) // compound assignment
    {                          
        /* addition of rhs to *this takes place here */
        return *this; // return the result by reference
    }
    

Upvotes: 1

Related Questions