Return Value of Operator Overloading in C++

return value of operator overloading in C++

Some operators return by value, some by reference. In general, an operator whose result is a new value (such as +, -, etc) must return the new value by value, and an operator whose result is an existing value, but modified (such as <<, >>, +=, -=, etc), should return a reference to the modified value.

For example, cout is a std::ostream, and inserting data into the stream is a modifying operation, so to implement the << operator to insert into an ostream, the operator is defined like this:

std::ostream& operator<< (std::ostream& lhs, const MyType& rhs)
{
// Do whatever to put the contents of the rhs object into the lhs stream
return lhs;
}

This way, when you have a compound statement like cout << x << y, the sub-expression cout << x is evaluated first, and then the expression [result of cout << x ] << y is evaluated. Since the operator << on x returns a reference to cout, the expression [result of cout << x ] << y is equivalent to cout << y, as expected.

Conversely, for "string + string", the result is a new string (both original strings are unchanged), so it must return by value (otherwise you would be returning a reference to a temporary, which is undefined behavior).

does operator overloading in C++ really expect a return value?

The last one is a conversion operator, so it is implied that it returns a float - you convert your value to this type.

As for operator<, it has return type because you can actually make it whatever you like. For instance, operator<< for C++ standard library streams do I/O instead of logical shifting.

c++ operator += overloading return reference to

You should return a reference because thats a convention most code in standard library is using, and most programmers do. Most programmers will expect below code:

std::string s;
(s += "a") = "b";
std::cout << s << std::endl;

to print b, or in this example:

int n = 10;
(n += 20) = 100;
std::cout << n << std::endl;

will expect 100 to be printed.

That is why you should return a reference to keep with the convention which allows to modify object which is on the left side of assignment.

Otherwise, if you return by value (a copy) assignment as in above examples will assign to a temporary.

Adding same class object return values using operator overloading in C++

You're trying to overload operator + whereas you're executing += where you had commented.

Just change the operator overloading sign from + into += and rather than writing:

void UserInterface::setMemory()
{
//What I want to essentially do
mCounter.returnCount() += aCounter.returnCount();
}

Since the operator overloading you've coded asks for an instance of a class, not an integer which is returned by the function returnCount() and that must be a modifiable lvalue (roughly the values which could be assigned with an = sign) value.

Do:

void UserInterface::setMemory()
{
mCounter += aCounter; // overloading class instance rather than int
}

return type of operator+= while overloading

Another reason why you would want operator += to return a reference to the current object is when you want to overload operator +. Since you're writing an integer class, it won't make a lot of sense if += were available, but + wasn't.

Here is what operator + would look like:

MyInteger MyInteger::operator+(const MyInteger& rhs)
{
MyInteger temp(*this);
return temp += rhs; // <-- Uses operator +=
}

The above could not work (or even compile) if operator += didn't return a reference.

Return value of assignment operator overloading c++

A pointer is a data type that holds a memory address of the type the pointer points to, or nullptr if it doesn't refer to anything. (Or a dangling pointer, or a garbage value... but that's a bad place.)

So dereferencing a pointer by *this means that you are now dealing with the object pointed to by the pointer. Which is why this->foo and (*this).foo do the same thing.

Why does C++ have a this pointer, rather than (say) a self reference? That's just how C++ evolved, which Bjarne Stroustrup discusses at length in his excellent book The Design and Evolution of C++.

Back to your operator= situation. In C++, you can typically do this kind of construct: x = y = z;, which is ordered like x = (y = z); due to the right-to-left association of assignment.

If your assignment was void operator=(Fraction const&) then it could not be used to do assignment chaining. Not supporting assignment chaining would be "friction" for anyone used to the expected behavior of assignment and how C++ built-in types allow chaining.

Why return Fraction& versus Fraction object, is more than a simple performance optimization. Because if you did (x = y).negate(); the hypothetical negate method would be operating on a temporary rather than on x.

Why we use reference return in assignment operator overloading and not at plus-minus ops?

Returning a reference from assignment allows chaining:

a = b = c;  // shorter than the equivalent "b = c; a = b;"

(This would also work (in most cases) if the operator returned a copy of the new value, but that's generally less efficient.)

We can't return a reference from arithmetic operations, since they produce a new value. The only (sensible) way to return a new value is to return it by value.

Returning a constant value, as your example does, prevents move semantics, so don't do that.



Related Topics



Leave a reply



Submit