C ++ is a problem in the function return value (rpm) of an object

Original link: https://www.cnblogs.com/yanhai307/p/10935665.html

 

Reference links:

https://www.cnblogs.com/mini-coconut/p/8542560.html

 https://www.cnblogs.com/ysherlock/p/7822287.html

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: the implementation of the constructor, my address is the p-% \ the n- " , name, the this );
    }

    Test(const Test &obj)
    {
        this->name = obj.name;
        printf ( " % S: the implementation of the copy constructor, my address is% p, copy from the p-% S% \ the n- " ,
               name.c_str(), this, obj.name.c_str(), &obj);
    }

    ~Test()
    {
        the printf ( " % S: performing destructor, My address is P% \ n- " , name.c_str (), the this );
    }

public:
    string name;
};

Test fun()
{
    T the Test ( " I was created in fun function " );
    printf("in fun: %p\n", &t);
    return t;
}

void test1()
{
    // Here is the fun t1 objects created inside a function? 
    << COUT " Fun .. Start " << endl;
    Test t1 = fun();
    cout << "fun end.." << endl;
    t1.name = " I was created in the test function " ;
    printf ( " I am the object is created in the test function, my address is: the p-% \ the 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..
I was created in a fun function: implementation of the constructor, my address is 010FFAC4
in fun: 010FFAC4
I was created in the fun in function: the implementation of the copy constructor, my address is 010FFBD4, copy 010FFAC4 from me is created in the fun in function
I was created in the fun in function: the implementation of the destructor, my address is 010FFAC4
fun end..
I am the object is created in the test function, my address is: 010FFBD4
I was created in the test function: the implementation of the destructor, my address is 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, it 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..
I was created in a fun function: implementation of the constructor, my address is 0x7ffe5a2488c0
in fun: 0x7ffe5a2488c0
fun end..
I am the object is created in the test function, my address is: 0x7ffe5a2488c0
I was created in the test function: the implementation of the destructor, my address is 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/lh03061238/p/12580907.html