1. What is the following program output? why?
#include <stdio.h>
class Test
{
int mi;
public:
Test(int i)
{
mi = i;
}
Test()
{
Test(0); //直接调用了构造函数,会产生一个临时对象;Test(0)这个构造函数对这个临时对象进行初始化后。就被析构函数销毁了。所以对这个程序而言。这句话没有任何意义。才导致了,打印的随机值。
}
void print()
{
printf("mi = %d\n", mi);
}
};
int main()
{
Test t;
t.print();
return 0;
}
Thinking:
Program intended: in Test () call as a parameter 0 Test (int i), the initial value setting member variable to 0 for mi;
run results: mi is a random value
answer:
. a direct call the constructor will produce a temporary object;
Time b lifecycle temporary objects only one statement
. c Scope temporary object only in a single statement
eg:
#include <stdio.h>
class Test
{
int mi;
void init(int i) //在实际工程中往往会提供一个私有的init函数来做初始设置
{
mi = i;
}
public:
Test(int i)
{
printf("Test(int i)\n");
init(i); //调用init初始函数
}
Test()
{
printf("Test()\n");
init(0); //调用init初始函数
}
void print()
{
printf("mi = %d\n", mi);
}
~Test()
{
printf("~Test()\n");
}
};
int main()
{
printf("main begin\n");
/* 通过这段代码更加的说明了上述答案中所述的三项
*/
Test(); //Test().print;
Test(0); //Test(0).print;
printf("main end\n");
return 0;
}
2. The compiler behavior
Modern c ++ compilers without affecting the final results of the implementation, will try to reduce temporary objects.
#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(10); //Test t(10); 等价于 Test t = Test(10);
//理论上 1.生成临时对象; 2.用临时对象初始化t对象; 3.调用拷贝函数
//实际上Test t = Test(10); ==》(编译器把这里优化为) Test t = 10; 不会产生临时对象
//实际工程中推荐使用Test t = 10这种写法,不推荐使用Test t(10)
Test tt = func(); //Test tt = func(); ==》Test t = Test(20);==》Test t = 20; 不会缠身临时对象
t.print();
tt.print();
return 0;
}
Test t[] = {Test(), Test(10), Test[10]}; //与上述类似
3. Summary
Direct, simply call the constructor will generate temporary objects;
Test(10);
Indirect call the constructor will be optimized compiler does not generate temporary function;
Test t = Test(10);
Test t[] = {Test(), Test(10), Test[20]};