When main() calls a function which has a return value of some datatype ( primitive or user-defined ) , the statement in which the function is called is 'usually' an assignment .
Ex :-
class complex
{
private:
int real;
int imag;
public:
complex();
complex(int r,int i);
complex operator + (complex c);
};
Suppose , the definition of my overloaded " + " is like this -
complex complex::operator + (complex c)
{
this->real = this->real + c.real;
this->imag = this->imag + c.imag;
return *this;
}
I have my main function as follows -
int main()
{
complex c1;
complex c2(5,4);
c1+c2;
}
In the main() above , consider the statement c1+c2 . The compiler treats it as c1.operator + (c2) . When this function is called by main , it returns a value to main() . What happens to this return value ??
The value of the expression
c1+c2
is ignored by your code, as you are not storing it anywhere. At most, the compiler will print some warning messages. To supress such warning messages, you could write :See this:
The real issue with your code..
However, in your code, the implementation of
operator+
is not semantically correct. To understand that consider this,then, do you expect
a+b
to change the value ofa
? Shoulda
become15
? No.If you want that, then you would write
a+=b
instead. But in your code,c1+c2
behaves equivalent to the semantic ofc1+=c2
, as you're updating the value ofthis->real
andthis->imag
in your implementation ofoperator+
, which is not correct, semantically.So the first fix is this:
Now, this is semantically correct.
That said, there is still few things to be noted. When you write
c1+c2
, do you think the operation+
is applied on either of the object? No. It doesn't apply on any of them, yet the member functionoperator+
gets invoked onc1
object which becomesthis
pointer inside the function. Why should it be invoked onc1
(or for that matterc2
) if the operation doesn't apply on it?This analysis makes it clear that
operator+
shouldn't be a member function of the class. It should be a non-member function instead, and the signature then would be:But there is a small problem: in the computation of
a+b
, it needs to access to the private members of the class (real
andimag
are private members). So the solution is,operator+
should be implemented in terms ofoperator+=
, and the latter should be added as a member function to the class, because the operation+=
in the expressiona+=b
does apply ona
, as it modifies its value.So here is my implementation of both operators:
Or you could join last two statements as:
But there is another way to make copy. Why pass both arguments by reference? Passing the first argument by value would make the copy we need. So a better implementation would be this:
Hope that helps.
The
return
value is discarded; as you are not storing it anywhere.One important advice:
Ideally the definition for
operator +
should look like.In your original code 2 copies of
complex
are made; among which at least 1 can be avoided with above format where the argument is pass by const reference.Also, in your code you should not change the current object; (otherwise it becomes like
operator +=()
). So create a temporary inside the function and pass it by value.Another solution is to use
operator +
in global scope but as a friend of your class:Using this technique you can also use an integer as argument (at the lhs) by adding:
So now you can also do
and your class is missing a (virtual) destructor, I would also make the member variables protected instead of private :)
It gets assigned into a temporal (invisible if you may) complex value. The lifetime of such value is until the expression that generated it ends, that's the ; at the end of c1+c2. So a new temporal object is created to store the result of the expression, and its destructed at the end of that line.
You shouldn't be modifying 'this' in your a + b operator, after evaluating a + b now a holds the result of the expression.
In this case, it is silently dropped (but the sum is stored in
c1
orc2
). The compiler might (or might not) optimise the code by dropping the line entirely, because it doesn't do anything substantial. The resulting sum will be constructed and returned by theoperator+
, (a temporary variable will be created) and then destroyed immediately.This happens in other cases, too. Consider this:
You can chain several additions like this together:
This is because
operator+=
also returns a value, but it is ignored like in your code.By the way, I think you want to use
operator+=
.It's simply discarded, but AFAIK C++ will show error when a function expecting return value doesn't have return statement.