Consider the following class.
If I make a call in main as:Code:class complex { private: int r,i; public: complex operator + (complex c2) { r = r + c2.r; i = i + c2.i; return *this; } ... };
(assuming c1,c2,c3 are all declared and initialized)Code:c3 = c1 + c2
then, the overloaded function would return the object c3 itself, right?
What if I change the function to return a reference?
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?Code:complex& operator + (complex c2) { r = r + c2.r; i = i + c2.i; return *this; }



1Likes
LinkBack URL
About LinkBacks




CornedBee