I do not understand the use of return *this "as a whole"
c++
Solution
Trivia
The function is returning a reference to a type that's the same as itself, and it returns... itself.
The pointer type
Because the returned type is a reference type(`Sales_data&`), and `this` is a pointer type(`Sales_data*`), you have to dereference it, thus `*this`, which actually is the reference to the object we're calling the member function on.
Usage
What it really allows is method chaining.
Sales_data total;
Sales_data a, b, c, d;
total.combine(a).combine(b).combine(c).combine(d);
It's sometimes written vertically:
total
.combine(a)
.combine(b)
.combine(c)
.combine(d);
And I am pretty sure you saw it already:
cout << "Hello" << ' ' << "World!" << endl;
In the above case, overloaded `operator<<` returns a reference to the output stream.
Problem
I cannot figure out what the below part of my book I'm reading actually does. ``` //this function supposed to mimic the += operator Sales_data& Sales_data::combine(const Sales_data &rhs) { units_sold += rhs.units_sold; // add the members of rhs into revenue += rhs.revenue; // the members of ''this'' object return *this; // return the object on which the function was called } int main() { //...sth sth Sales_data total, trans; //assuming both total and trans were also defined... total.combine(trans); //and here the book says: //we do need to use this to access the object as a whole. //Here the return statement dereferences this to obtain the object on which the //function is executing. That is, for the call above, we return a reference to total. //*** what does it mean "we return a reference to total" !? } ``` I should say that I have previously a little knowledge in C# and don't really understand how exactly `return *this;` affects total object.