简体   繁体   English

函数在C ++中返回“This”对象

[英]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& 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. 我不理解的是函数返回*this ,我理解它会返回一个对象的实例,但它不会将该实例返回给我们在函数调用期间可以看到的任何内容,如果我不写return语句,它的工作方式不同。

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被更改并返回对x引用,因此您有机会通过再次在prevous步骤上返回的引用再次调用此更改实例上的combine()

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

Another popular example is operator=() implementation. 另一个流行的例子是operator=()实现。 So, if you implement operator= for a custom class it is often a good idea to return a reference to the instance: 因此,如果为自定义类实现operator= ,通常最好返回对实例的引用:

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: 使用return语句定义函数的一个好处是它允许连续调用,如下面的代码:

// 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 . 但是,如果您不需要或不想使用早期版本,则可以声明函数返回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. 另外,如果我不写return语句,它会以不同的方式工作。

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. 但是你肯定可以在不改变这个特定程序的功能的情况下使返回类型为void

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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