class stu{
public:
int age;
int score;
stu() = default;
stu(int a, int s):
age(a), score(s) { cout << " i am constructor" << endl;}
stu(const stu& stu1):
age(stu1.age), score(stu1.score) { cout << " i am copy constructor" << endl;}
};
const stu combine(const stu &stu1, const stu &stu2)
{
stu new_stu(stu1.age + stu2.age, stu1.score + stu2.score);
return new_stu;
}
int main()
{
stu stu1(22,100);
stu stu2(23,99);
stu result(combine(stu1, stu2));
cout << result.age << result.score << endl;
}
我的理解是应该创建了四个对象,其中一个是临时对象,由combine(stu1, stu2)函数返回时创建。类的构造函数执行打印一行字符串的功能,通过有多少行字符串可以推出执行了多少次构造函数,代码在运行时只有三行字符串,意味着只创建了三个对象,究竟哪里有问题??
First of all, there is a correction. There is nothing wrong with returning a temporary object from
combine
, but the copy constructor is called here, not the constructor. Then the constructor is called again whenresult
is declared, So the final number is two constructors + one copy constructor + one constructorThen the question arises, why is the copy constructor not reflected at all? This is because the compiler will skip the copy constructor and directly execute the constructor, so the result becomes three times. Constructor, for related content, please refer to
c++ primer
Chapter 13.Although the compiler will skip the copy constructor, you must ensure that the copy constructor of the class is accessible (not private). If you verify this, an error will be reported.
This is RVO (Return Value Optimization)
Reference: https://en.wikipedia.org/wiki/Return_value_optimization
Five objects were generated
stu1 +1
stu2 +1
new_stu +1
const stu +1
result +1
The compiler will enable return value optimization by default
You can turn off this optimization by adding the -fno-elide-constructors option when compiling with gcc.
The result of running the compiled program is as follows:
i am constructor
i am constructor
i am constructor
i am copy constructor
i am copy constructor
45199