因为我对这个问题的疑虑(对于C++ 03)我张贴它只是这儿过得阅读转换构造,并指出拷贝构造函数和一个转换构造之间的困惑
“要是一个转换构造函数,构造函数必须具有单个 参数,并且在没有显式关键字的情况下声明。“
现在我的问题是复制构造函数是否可以被称为转换构造函数提供它没有明确声明?它有资格成为一个吗? 我相信它不能被称为转换构造函数,因为它只接受相同的类型参数ths导致没有转换。例如
foo a;
foo b;
a = 100; //a Conversion constructor would be called (i.e) foo(int a){...}
a = b ; //Since both objects are same type and have been initialized the assignment operator will be called (if there is an overloaded version otherwise the default will be called)
我的理解是否正确?
如果可能,'operator =()'将是首选,但真正的问题是“当找不到合适的'operator =()'实现时会发生什么?” – cdhowie
foo a = 100; //是转换结构 foo b = a; //是复制构造 –
[class.conv.ctor]/3“非显式拷贝构造函数(12.8)是一个转换构造函数,隐式声明的拷贝构造函数 不是一个显式的构造函数;它可以被隐式调用类型转换“。 – dyp