move constructor and copy constructor in C++

后端 未结 2 1161
轮回少年
轮回少年 2021-01-14 11:52

My understanding is that a move constructor is called if it exists when we return a local object from a function. However, I ran into a situation where the copy constructor

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 12:21

    tNode foo()
    {
        tNode node;
        return node;
    }
    

    and

    tNode n1 = foo();
    

    Is responsible for the output of

    a: 10,  tNode() is called at testCopyControl.cpp:13
    a: 10,  move constructor tNode() is called at testCopyControl.cpp:31
    a: 10, destructor ~tNode() is called at testCopyControl.cpp:40
    a: 10,  move constructor tNode() is called at testCopyControl.cpp:31
    a: 10, destructor ~tNode() is called at testCopyControl.cpp:40
    

    And what you see is the default constructor being called and then node begin treated as an rvalue in the return statement to move it into the return value and then another move from the return value into n1

    With

    tNode foo2()
    {
        std::unique_ptr up = std::make_unique(20);
        return *up;
    }
    

    The behavior is different as you are not returning a function local object. *up gives you a tNode& so the return statement can't treat it as an rvalue. Since it is an lvalue you have to call the copy constructor to copy it into the return value. Then, like the first example, the move constructor is called to move the object from the return value into n2.

提交回复
热议问题