class example {
public:
example(string str){
cout <example() {
cout<example( example& rhs){
cout<};
int main (int argc, char* argv[])
{
string str = "123456789";
example e1(str);
example e2 = str;
example e3 = e1;
example e4 = example();
return 0;
}
这个例子来自C++Primer,在VS2010可以编译,gcc下e2 and e4报错:no matching
function for call to example::example(example)
如果在copy constructor里加上const,gcc可以编译。我的理解是初始化e2 and e4需要
产生临时对象,这个对象是右值,C++标准可能要求形参const以接受右值,为什么VS没
报错不明白。
但是除了e3,copy constructor在初始化e2和e4的时候没有调用,
而C++Primer说这种是copy-initialization,那为什么在VS和gcc下都没有调用copy
constructor?被优化了?
隐式调用copy constructor是不是有编译器决定?