What does return *this c++return?
Return *this returns the object itself, and MyClass & converts as an object reference (well, or MyClass & indicates that return *this (that is, the object) is a reference)?
Explain what and how and then I'm confused
MyClass & operator = (const MyClass& other)
{
// Код...
// Код...
// Код...
// Код...
return *this;
}
1 answers
Returns a reference to the caller to call the methods sequentially.
To avoid confusion, you need to understand how objects can be passed in C++. And they can be passed: by value, by pointer, and by reference.
Since this is a pointer, it must first be dereferenced. The reference behaves exactly like the object itself, its original. Therefore, this is also possible:
object.method() = variable;
In your case, the method is operator=(). Overloaded operators have associativity it is saved, so everything will work as it should:
object_1 = object_2 = object_3;
object_1.operator=(object_2.operator=(object_3));
Whereas, for example, operator+() will work by default with reverse associativity:
object_1 + object_2 + object_3;
object_1.operator+(object_2).operator+(object_3);