简体   繁体   中英

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?

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 . It turns out that the object c has persisted, which means it's not removed from the stack after execution of function operator+ . Or, the object c is in fact allocated in heap ??

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. The Box object in the operator+ function is copied into the object in the main function. Then the object in the operator+ function is destructed, leaving you with the object in the main function.

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++).

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()

The lifetime of the box variable in main() is tied to the surrounding scope, which in this case is the duration of main() . And the object is allocated on the stack for the function main() , which is where it lives.

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

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