简体   繁体   English

从C ++中的函数返回对象

[英]Returning an object from a function in C++

I have a small program in which I have a global function overloading the operator + : 我有一个小程序,其中有一个全局函数使运算符+重载:

class Box {
    public:
        Box (int, int);
        Box (const Box&);
        ~Box ();
        int get_width() const;
        int get_length() const;
    private:
        int width;
        int length;
};

Box operator+(const Box& a, const Box& b) {
    int w, l;
    w = a.get_width() + b.get_width();
    l = a.get_length() + b.get_length();
    return Box(w, l);
}

In the function operation+ , I have returned an object of class Box , as the object is not instantiated via operator new , the object is allocated on stack, isn't it? operation+函数中,我返回了Box类的对象,因为该对象未通过操作符new实例化,所以该对象分配在堆栈上,不是吗?

int main (int argc, char *argv[]) {
    Box a(100, 200);
    Box b(101, 202);

    Box c = a + b;
    cout << "width: " << c.get_width() << "; length: " << c.get_length() << endl;
    return 0;
}

In my main function, I tried to add 2 box a + b , and print the size of box c . 在我的main功能中,我尝试添加2个框a + b ,并打印框c的大小。 It turns out that the object c has persisted, which means it's not removed from the stack after execution of function operator+ . 事实证明,对象c已持久存在,这意味着在执行功能operator+之后并未将其从堆栈中移除。 Or, the object c is in fact allocated in heap ?? 或者,对象c实际上分配在堆中?

I'm confused, anyone who could explain this to me please? 我很困惑,有人可以向我解释一下吗?

What happens is basically that two Box objects are created: One in the main function and one in the operator+ function. 基本上发生的事情是创建了两个 Box对象:一个在main函数中,一个在operator+函数中。 The Box object in the operator+ function is copied into the object in the main function. operator+函数中的Box对象被复制main函数中的对象中。 Then the object in the operator+ function is destructed, leaving you with the object in the main function. 然后, operator+函数中的对象将被破坏,而main函数中将保留该对象。

A modern optimizing compiler do skip some of the steps mentioned above, most notable it will only create a single object as part of its return value optimizations (creating only a single object and not copy anything is called copy elision , a term you will come in contact with sooner or later when programming in C++). 现代的优化编译器做跳过上述一些步骤中提到,最显着的这只会造成一个对象作为其组成部分的返回值优化 (只创建一个单一的对象,而不是复制任何东西被称为复制省略 ,这个词你会在使用C ++进行编程时,请早晚联系。)

With respect to your code. 关于您的代码。 If you have a function like the following 如果您具有以下功能

Box foo() {
    auto box_one = Box{};
    auto box_two = Box{};
    return box_one + box_two;
}

int main() {
    auto box = foo();
    // use box
}

The local variable in this foo() function starts off in the stack for the function (see last paragraph below for more) and is copied (again see below) to the variable box which lives in the stack for main() foo()函数中的局部变量在函数的堆栈中启动(有关更多信息,请参见下面的最后一段),然后将其复制(再次参见下文)到位于main()的堆栈中的变量box

The lifetime of the box variable in main() is tied to the surrounding scope, which in this case is the duration of main() . main() box变量的生存期与周围的范围有关,在这种情况下,它是main()的持续时间。 And the object is allocated on the stack for the function main() , which is where it lives. 对象在栈中分配给函数main() ,该函数位于对象所在的位置。

What happens in most optimizing compilers (pre C++17) and in all standard conforming compilers after C++17 is that the original Box variable that is returned from the foo() function is placed directly where it should be in the callee's stack, so foo() s return object is in main() stack from from the beginning (assuming it's called from main() ) This process is known as elision 在大多数优化的编译器(C ++ 17之前的版本)和C ++ 17之后的所有符合标准的编译器中,将从foo()函数返回的原始Box变量直接放置在被调用方堆栈中应有的位置,因此foo()的返回对象从一开始就位于main()堆栈中(假设从main()调用了它)此过程称为省略

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

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