使用临时对象作为拷贝构造函数参数的问题
最近在看书遇到临时对象与const&的问题,以前一直没怎么注意这个细节,所以写了下面的程序作为测试
#include <iostream>
using std::cout;
using std::endl;
class A {
public:
explicit A():id(++num) {
cout<<"call A():"<<id<<endl;
}
A(const A& a):id(++num) {
cout<<"call A(const A&):"<<id<<endl;
}
~A(){
cout<<"call ~A():"<<id<<endl;
}
A& operator= (const A& a) {
cout<<"operator="<<endl;
cout<<"lvalue = A:"<<id<<endl;
cout<<"rvalue = A:"<<a.id<<endl;
return *this;
}
unsigned int id;
private:
static unsigned int num;
};
unsigned int A::num = 0;
A func1() {
cout<<"in func1()"<<endl;
A a;
cout<<a.id<<endl;
cout<<"out func1()"<<endl;
return a;
}
int main() {
A a4((A()));//语句①
cout<<"a4.id = "<<a4.id<<endl;
return 0;
}
call A():1
call A(const A&):2
a4.id = 2
call ~A():2
call ~A():1
call A():1
a4.id = 1
call ~A():1
A a4(func1())
in func1()
call A():1
1
out func1()
call A(const A&):2
call ~A():1
a4.id = 2
call ~A():2
in func1()
call A():1
1
out func1()
a4.id = 1
call ~A():1
1. A a4((A())); A a4(func1());
GCC应该是做了优化。
2. 另外将拷贝构造函数的const去掉后,GCC 4.7会编译错误,这符合预测。但是VS2008却没有出错,这是因为VS2008不符合标准吗?
看来VS2008对标准的支持还是不够好。LZ用VS2013试一下吧。
VS2013也一样……