g++有编译选项-fno-elide-constructors。提供下面的代码,供编译使用,查看使用该选项后的差别……也算抛砖引玉!
#include <iostream>
using namespace std;
class A
{
public:
A() {cout << "A created" << endl;}
A(A& a) {cout << "A created with copy" << endl;}
~A() {cout << "A destroyed" << endl;}
};
A foo(A a)
{
cout << "foo()--------------->" << endl;
A b;
cout << "foo()<---------------" << endl;
return b;
}
int main()
{
cout << "main()--------------->" << endl;
A a;
a = foo(a);
cout << "main()<---------------" << endl;
return 0;
}