0
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; 
}

What does return *this do?

Vice
  • 53
  • 8

3 Answers3

13

The return *this returns the current object (As a reference), which means you can now chain function calls on Sales_data if you called combine on a previous call. This means that something like this would work:

Sales_data t;
t.combine(a).combine(b).combine(c); // Assuming a, b, and c are other Sales_data's

This is a very useful technique which allows you to create a fluent interface which further allows for you to use things such as the named parameter idiom and is present in a variety of programming languages.

Arnav Borborah
  • 11,357
  • 8
  • 43
  • 88
1

It returns the pointer to current Sales_data object (on which this member function called) with updated values of units_sold and revenue.

I guess by this functionality you are trying to update the values in current sales data by combining another one.

Yes, as @Arnav said, it can be used for chaining as it always returns your object address.

Shan
  • 21
  • 3
0

It returns the object - in this case a reference to the one used

Ed Heal
  • 59,252
  • 17
  • 87
  • 127