<分区>
Possible Duplicate:
Why copy constructor is not called in this case?
在下面的代码中,我构造了三个变量a1、a2和a3。
C++ Primer p.476 中有一个示例:
string empty_copy = string();//copy-initialization
有没有人可以帮我解释一下
1)为什么a1和a2不是用拷贝构造函数构造的
2)我代码中的初始化a2和书上的empty_copy有什么区别?
非常感谢!
#include<iostream>
using namespace std;
class A{
public:
A(){}
A(int v){}
A(const A&x){
cout<<"copy constructor"<<endl;
}
};
A generateA(){
return A(0);
}
int main(){
cout<<"First:"<<endl;
A a1=generateA();
cout<<"Second:"<<endl;
A a2=A(0);
cout<<"Third:"<<endl;
A a3=a1;
return 0;
}
输出是(在Win7的Visual Studio 2010和Ubuntu10.10的g++下):
First:
Second:
Third:
copy constructor