[C ++ in-depth analysis learning summary] 23 undesirable temporary objects

[C ++ in-depth analysis learning summary] 23 undesirable temporary objects

Author CodeAllen , please indicate the source


1. Interesting question
What does the following program output? why?
Insert picture description here
Interesting question

#include <stdio.h>

class Test {
    int mi;
public:
    Test(int i) {
        mi = i;
    }
    Test() {
        Test(0);  //等价于没有,过了这一行就没了
    }
    void print() {
        printf("mi = %d\n", mi);
    }
};

int main()
{
    Test t;
    
    t.print();

    return 0;
}

2. What happened?
Procedural intention:

  • Call Test (int i) with 0 as a parameter in Test ()
  • Set the initial value of the member variable mi to 0 to
    run the result:
  • The value of the member variable mi is a random value
  • What went wrong? The appearance of temporary objects

3. Thinking that the
constructor is a special function

  • Can it be called directly?
  • Is it possible to call the constructor in the constructor?
  • What is the behavior of calling the constructor directly?

4. Answer

  • Calling the constructor directly will produce a temporary object
  • The life cycle of a temporary object is only one statement
  • The scope of temporary objects is only in one statement
  • Temporary objects are a grey area in C ++ that is worth warning

solution

#include <stdio.h>
class Test {
    int mi;
   
    void init(int i)  //私有的初始函数
    {
        mi = i;
    }
public:
    Test(int i) {
        init(i);   //用的时候调用,就不会产生临时对象
    }
    Test() {
        init(0);
    }
    void print() {
        printf("mi = %d\n", mi);
    }
};
int main()
{
    Test t;
   
    t.print();
    return 0;
}

5. Compiler behavior
Modern C ++ compiler will try its best to reduce the generation of temporary objects without affecting the final execution result! ! !

Temporary object

#include <stdio.h>
class Test
{
    int mi;
public:
    Test(int i)
    {
        printf("Test(int i) : %d\n", i);
        mi = i;
    }
    Test(const Test& t)
    {
        printf("Test(const Test& t) : %d\n", t.mi);
        mi = t.mi;
    }
    Test()
    {
        printf("Test()\n");
        mi = 0;
    }
    int print()
    {
        printf("mi = %d\n", mi);
    }
    ~Test()
    {
        printf("~Test()\n");
    }
};
Test func()
{
    return Test(20);
}
int main()
{
    Test t = Test(10); //预计运行过程: 1.生成临时对象 2.用临时对象初始化t对象
                       //==>调用拷贝构造函数,实际运行过程:==> Test t = 10; 推荐这么写就可以避免临时对象
    Test tt = func();  // ==> Test tt = Test(20); ==> Test tt = 20;
   
    t.print();
    tt.print();
   
    return 0;
}

Summary
Directly calling the constructor will generate a temporary object.
Temporary objects are a performance bottleneck and one of the sources of bugs.
Modern C ++ compilers will try their best to avoid temporary objects. In
actual engineering development, artificial objects need to be avoided.

Published 315 original articles · praised 937 · 650,000 views

Guess you like

Origin blog.csdn.net/super828/article/details/102509467