用一个临时对象初始化一个新构造的对象会不会调用复制函数?
用一个临时对象初始化一个新构造的对象会不会调用复制函数?比如说:
A a=A();
什么时候会调用复制构造函数,什么时候不会?
[解决办法]
cat main.cpp; g++ main.cpp -O3 -o demo.exe; ./demo.exe
#include <cstdio>
using namespace std;
class RVO
{
public:
RVO(){printf("I am in constructor @ %p\n", this);}
RVO (const RVO& c_RVO) {
printf ("I am in copy constructor src @ %p this@ %p\n", &c_RVO, this);
}
~RVO(){printf ("I am in destructor @%p\n", this);}
int mem_var;
};
RVO MyMethod (int i)
{
RVO rvo;
rvo.mem_var = i;
return (rvo);
}
int main()
{
RVO rvo=MyMethod(5);
printf("rvo @ %p\n", &rvo);
return 0;
}
/*
I am in constructor @ 0022FF0C
rvo @ 0022FF0C
I am in destructor @0022FF0C
*/