class X
{
int i;
public:
X(int ii=0);
X(X &a);
~X();
void modify();
};
X::X(int ii)
{
i=ii;
cout<<"constructor "<<this->i<<endl;
}
X::X(X &a)
{
i=a.i;
cout<<"copy "<<this->i<<endl;
}
X::~X()
{
cout<<"destructor "<<this->i<<endl;
}
void X::modify()
{
i++;
}
X f5()
{
return X();
}
void main()
{
X a(10);
f5()=a;
}
/////////////////////////////
结果是:
constructor 10 //对象a构造函数
constructor 0 //f5()函数中X()构造函数
destructor 10 //析构
destructor 10 //析构
编译器不是要产生一个临时对象来保存f5()返回值吗??
可是从结果上看并没有调用构造函数或拷贝构造函数来产生一个临时变量来接受对象a的赋值啊????