我的类结构如下:
class P {
public:
virtual std::auto_ptr<P> clone() const=0;
};
class A : public P {
public:
std::auto_ptr<P> clone() const {
return std::auto_ptr<P>(new A(*this));
}
};
class B : public P {
public:
std::auto_ptr<P> clone() const {
return std::auto_ptr<P>(new B(*this));
}
};
class C : public P {
public:
std::auto_ptr<P> clone() const {
return std::auto_ptr<P>(new C(*this));
}
};
这只是复制构造函数的代码:类 A
、B
、C
在其他方面都有不同的代码。这里有很多代码重复,能不能简化一下?
最佳答案
使用 CRTP,您可以:
template <typename Derived, typename Base>
struct Clonable : public Base
{
std::auto_ptr<Base> clone() const {
return std::auto_ptr<Base>(new Derived(static_cast<const Derived&>(*this)));
}
};
class A : public Clonable<A, P> {};
class B : public Clonable<B, P> {};
class C : public Clonable<C, P> {};
为了使派生类的实型受益,我会将代码修改为:
class P {
public:
std::auto_ptr<P> clone() const { return std::auto_ptr<P>(vclone()); }
private:
virtual P* vclone() const = 0;
};
template <typename Derived, typename Base>
struct Clonable : public Base
{
std::unique_ptr<Derived> clone() const {
return std::unique_ptr<Derived>(static_cast<Derived*>(vclone()));
}
private:
// Cannot use covariant-type `Derived*` in CRTP as Derived is not complete yet
Base* vclone() const {
return new Derived(static_cast<const Derived&>(*this));
}
};
class A : public Clonable<A, P> {};
class B : public Clonable<B, P> {};
class C : public Clonable<C, P> {};
因此,以下是有效的:
A a;
std::auto_ptr<A> a_copy = a.clone();
关于c++ - 可以在基类中定义复制构造函数并仍然处理派生类情况吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59414984/