Consider the following class.
Code:
class complex
{
private:
int r,i;
public:
complex operator + (complex c2)
{
r = r + c2.r;
i = i + c2.i;
return *this;
}
...
};
If I make a call in main as:
(assuming c1,c2,c3 are all declared and initialized)
then, the overloaded function would return the object c3 itself, right?
What if I change the function to return a reference?
Code:
complex& operator + (complex c2)
{
r = r + c2.r;
i = i + c2.i;
return *this;
}
What is the difference between the two? Does the first function cause a copy of the object pointed to by *this to be created and then copied back to c3? And what about in the latter case?Am I missing something here?