Function returns "This" object in C ++

So the next one is the member function of the Sales_data class, which is defined outside the class,

Sales_data& Sales_data::combine(const Sales_data &rhs) {
    units_sold += rhs.units_sold;
    revenue += rhs.revenue; //adding the members of rhs into the members of "this" object
    return *this;
} //units_sold and revenue are both data members of class

      

When the function is called, it is called like

total.combine(trans); //total and trans are the objects of class

      

What I don't understand is a function that returns *this

, I understand that it returns an instance of an object, but it does not return that instance to anything, as we can see during the function call, also if I don't write a return statement, it will does it work differently.

Someone please explain in detail because I just don't understand.

+3


source to share


3 answers


This is the usual way to do the following construction work:

Sales_data x, y, z;
// some code here
auto res = x.combine(y).combine(z);

      

When you call:

x.combine(y)

      

x

, and a reference to is returned x

, so you can call combine()

on this modified instance using the reference returned in the previous step again:

x.combine(y).combine(z);

      




Another popular example is implementation operator=()

. So, if you are implementing operator=

for a custom class, it is often a good idea to return an instance reference:

class Foo
{
public:
    Foo& operator=(const Foo&)
    {
        // impl
        return *this;
    }
};

      

What makes the assignment work for your class as it works for standard types:

Foo x, y, z;
// some code here
x = y = z;

      

+5


source


One of the benefits of defining a function that you define with an operator return

is that it allows sequential calls such as the code below:

// assuming that trans1 and trans2 are defined and initialized properly earlier
total.combine(trans1).combine(trans2);
// now total contains values from trans1 and trans2

      

This is equivalent to:



// assuming that trans1 and trans2 are defined and initialized properly earlier
total.combine(trans1);
total.combine(trans2);
// now total contains values from trans1 and trans2

      

but it is a little more concise and concise.

However, if you don't need or want to use an earlier version, you can declare a function to return void

.

+2


source


I understand that it returns an instance of an object, but it does not return that instance to anything as we can see during the function call

That's right, in this case.

But you can use the return value if you like. It's there just in case.

This is a common convention for chaining function calls.

Also if I don't write the return statement, would it work differently.

It will have undefined behavior because the function has a return type and therefore must return something. But you can make the return type void

without changing the functionality of that particular program.

+1


source







All Articles