使用以下代码:
以下情况是正确的:
我的问题是:是否有一种方法可以获得这样的隐式复制构造函数,但不必定义它的工作方式?当一个类被创建时默认情况下提供复制、赋值和标准构造函数...如果可能的话,使用模板时也能免费获得隐式转换构造函数将会很好。
我猜这是不可能的,但如果是这样,请问有人能解释一下为什么吗?
谢谢!
template <typename T>
class MyClass
{
public:
MyClass(const MyClass& other)
{
//Explicit, types must both match;
}
template <typename U>
MyClass(const MyClass<U>& other)
{
//Implicit, types can copy across if assignable.
//<?> Is there a way to make this automatically happen for memberwise
//copying without having to define the contents of this function?
this->value = other.getValue();
}
privte:
T value;
};
以下情况是正确的:
MyClass<float> a;
MyClass<float> b = a; //Calls explicit constructor which would have been auto-generated.
MyClass<int> c;
MyClass<float> d = c; //Calls implicit constructor which would not have been auto gen.
我的问题是:是否有一种方法可以获得这样的隐式复制构造函数,但不必定义它的工作方式?当一个类被创建时默认情况下提供复制、赋值和标准构造函数...如果可能的话,使用模板时也能免费获得隐式转换构造函数将会很好。
我猜这是不可能的,但如果是这样,请问有人能解释一下为什么吗?
谢谢!