简体   繁体   中英

Function returning “This” object in C++

So, following is a member function of Class Sales_data 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 am not understanding is the function returning *this , i understand it returns an instance of the object but it is not returning that instance to anything as we can see during function call, also if i don't write the return statement, would it work any differently.

Someone please explain elaborately because i am just not getting it.

It is a common way to make the next construction work:

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

When you call:

x.combine(y)

x is changed and reference to x is returned, so you have an opportunity to call combine() on this changed instance via the reference returned on the prevous step once more time:

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

Another popular example is operator=() implementation. So, if you implement operator= for a custom class it is often a good idea to return a reference to the instance:

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

Which makes an assignment work for your class as it works for standard types:

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

One benefit of defining the function as you have defined with return statement is that it allows consecutive calls like the code below possible:

// 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 succinct and brief.

However, if you do not need or like to use the earlier version, you can declare the function to return void .

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

Correct, in this case.

But you could use the return value if you wanted to. It's there just in case.

This is a common convention, allowing for function call chaining .

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

It would have undefined behaviour, because the function has a return type and therefore must return something . But you could make the return type void without altering the functionality of this particular program, sure.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM