问题如下,有经历的人自然懂得在下的问题
[code="c++"]
#include
using std::cout;
using std::endl;
class Test
{
public:
static int counter;
char identifier;
public:
inline Test(char para){ identifier='A'+(Test::counter++);cout<<"invoking constructor : "<identifier<<endl; }
inline Test(Test ¶){ identifier='A'+(Test::counter++);cout<<"invoking copy constructor "<"<<identifier<<endl; }
inline ~Test(){ cout<<"invoking deconstructor "<identifier<<endl; }
inline Test operator =(Test ¶){ cout<<"invoking operator = "<<endl;return para;}
};
int Test::counter=0;
Test f(Test para){ cout<<"invoking function f(Test) "<<endl; return para; }
int main()
{
//cout<<"============ 1 ==============="<<endl;
//Test a(1);
//cout<<"============ 2 ==============="<<endl;
Test b(f(Test(2)));//此步 临时变量的作参数 和 函数返回值 都没有调用复制构造函数
//cout<<"============ 3 ==============="<<endl;
//a=f(a);//对 内存变量的操作 则 传参数 和 函数返回值 都调用了复制构造函数
//cout<<"=========== over ============="<<endl;
return 0;
}
[/code]
IDE:VS2008;编译器版本:VC9.0
[code="java"]Test b(f(Test(2)));//此步 临时变量的作参数 和 函数返回值 都没有调用复制构造函数[/code]
这个是因为编译器优化的原因。详见:
http://msdn.microsoft.com/zh-cn/library/ms364057%28VS.80%29.aspx