In my C++ class they were discussing how to create an assignment operator. At the end of the assignment was the line “return *this,” which they said returned a reference to the object that “this” points to. Why does it return a reference? If “this” is being dereferenced, shouldn’t it just return the object?
Share
A function returns a reference if its declaration (i.e. its signature) tells so.
So (assuming a
class Foo) if a function is declaratedthen it returns a value (with copying, etc..)
But if it is declared as
or as
a reference is returned.
The statement
return *this;don’t define by itself if a reference or a value is returned.