首先看一下下面的程序:
#include
<
iostream
>
using namespace std;
class A
... {
public:
A()
...{
cout << "A()"<<endl;
}
A(const A &rhs)
...{
cout << "A(const A &rhs)"<<endl;
}
~A()
...{
cout<< "~A()"<<endl;
}
} ;
A foo()
... {
A a;
return a;
}
int main()
... {
...{
A b = foo();
}
system("pause");
}
using namespace std;
class A
... {
public:
A()
...{
cout << "A()"<<endl;
}
A(const A &rhs)
...{
cout << "A(const A &rhs)"<<endl;
}
~A()
...{
cout<< "~A()"<<endl;
}
} ;
A foo()
... {
A a;
return a;
}
int main()
... {
...{
A b = foo();
}
system("pause");
}
VS2005的Debug版本输出:
A()
A(const A &rhs)
~A()
~A()
VS2005的Release版本输出:
A()
~A()
在程序里,函数foo产生一个临时对象a,然后将其返回。
A b=foo();表示用foo产生的临时对象,来调用类A的拷贝构造函数。
在Release版本中,函数foo里的临时对象被优化掉了,就像它从来没出现过一样。这并不是说,不提供那个拷贝构造函数,程序也没问题。对于深拷贝来说,不提供拷贝构造函数,是错的。
注1:DevCPP默认的就是优化后的,所以当看到显示没调用拷贝构造函数的时候,我还因为我的理解错了,吓了一跳。DevCPP和VC2005的差别还是有一些的,比如将以上代码的拷贝构造函数的形参改为A& rhs,在DevCPP里报错,在VC2005里能运行。
DevCPP报错我可以理解,因为A b=foo();中,foo返回的是一个临时对象,这个临时对象是用户不能改变的,所以要获得它的引用,必须要用一个const引用来接受它(见《C++Primer》第三版P87)。VC2005为什么能运行,我就不知道了。
注2:有关NRV的内容,可以参考《深度探索C++对象模型》