In C ++ function return value is an object problem

Problem Description

In C ++ program, a function return value is an object, returns the local variables inside the function itself,
or will produce an intermediate object (anonymous object) it?

After testing, the win and Linux platforms effects of different

code show as below

//
// Created by YANHAI on 2019/5/28.
//
#include <iostream>
using namespace std;

class Test {
public:
    Test(const char *name)
    {
        this->name = name;
        printf("%s: 执行了构造函数, 我的地址是 %p\n", name, this);
    }

    Test(const Test &obj)
    {
        this->name = obj.name;
        printf("%s: 执行了拷贝构造函数,我的地址是 %p,拷贝来自%s %p\n",
               name.c_str(), this, obj.name.c_str(), &obj);
    }

    ~Test()
    {
        printf("%s: 执行了析构函数, 我的地址是 %p\n", name.c_str(), this);
    }

public:
    string name;
};

Test fun()
{
    Test t("我是在fun函数中创建的");
    printf("in fun: %p\n", &t);
    return t;
}

void test1()
{
    // 这里t1对象就是fun函数里面创建的?
    cout << "fun start.." << endl;
    Test t1 = fun();
    cout << "fun end.." << endl;
    t1.name = "我是在test函数中被创建的";
    printf("我是在test函数中被创建的对象,我的地址是: %p\n", &t1);
}

int main()
{
    cout << "--------test1 start ...-----" << endl;
    test1();
    cout << "--------test1 end ...-----" << endl;
    return 0;
}

Testing process

In the win platform

Using VS2019 compile and run

operation result:

--------test1 start ...-----
fun start..
我是在fun函数中创建的: 执行了构造函数, 我的地址是 010FFAC4
in fun: 010FFAC4
我是在fun函数中创建的: 执行了拷贝构造函数,我的地址是 010FFBD4,拷贝来自我是在fun函数中创建的 010FFAC4
我是在fun函数中创建的: 执行了析构函数, 我的地址是 010FFAC4
fun end..
我是在test函数中被创建的对象,我的地址是: 010FFBD4
我是在test函数中被创建的: 执行了析构函数, 我的地址是 010FFBD4
--------test1 end ...-----

Explain the process:

  1. In fun function, t object is created, the object constructor function performed t (t target address 010FFAC4)
  2. When fun return function is executed, generates an anonymous object, the object executes the anonymous copy constructor, equivalent to the implementation of the Test tmp = t; (anonymous objects tmp address 010FFBD4)
  3. fun function execution ends, the object local variable t is released, t performed destructor object, fun function anonymous objects (tmp) return (return address is anonymous object 010FFBD4)
  4. In test1 function, t1 when the object is created using the return value of the function fun, it becomes anonymous object tmp t1 objects directly (rather than performing a copy constructor t1, to perform such a Test t1 = Test ( "xx") ;) address (t1 anonymous object is the object address 010FFBD4)
  5. After test1 function completes, the object is released t1, t1 performed destructor

In linux platform

Use g ++ compiler

operation result:

--------test1 start ...-----
fun start..
我是在fun函数中创建的: 执行了构造函数, 我的地址是 0x7ffe5a2488c0
in fun: 0x7ffe5a2488c0
fun end..
我是在test函数中被创建的对象,我的地址是: 0x7ffe5a2488c0
我是在test函数中被创建的: 执行了析构函数, 我的地址是 0x7ffe5a2488c0
--------test1 end ...-----

Explain the process:

  1. In fun function, t object is created, the object constructor function performed t (t target address 0x7ffe5a2488c0)
  2. At the end of the function fun, and no anonymous object, but the object returned t (t 0x7ffe5a2488c0 return is the object address)
  3. In test1 function, t1 object uses fun when the function return value is created, it returns the object t becomes t1 objects directly (rather than performing a copy constructor t1, to perform such a Test t1 = Test ( "xx") ;) address (t1 t is the target object address 0x7ffe5a2488c0)
  4. After test1 function completes, the object is released t1, t1 performed destructor

in conclusion

  1. In linux platform, it produced a less anonymous objects, improve the efficiency
  2. Fun only within the original function (lifetime local variables) t of the object, since the return function remains valid test1

Guess you like

Origin www.cnblogs.com/yanhai307/p/10935665.html