如果要在C ++中实现Clone
模式,他可能不确定安全性,因为派生类可能会忘记覆盖它:
struct A {
virtual A* Clone() const {
return new A(*this);
}
}
struct B : A {
int value;
};
int main() {
B b;
// oops
auto b_clone = b.Clone();
delete b_clone;
}
在这方面,可以使用哪些方法来改善C ++中的Clone
模式?
提出了一个更一般的问题: Forcing a derived class to overload a virtual method in a non-abstract base class
但是,在C ++中找到好的解决方案似乎太笼统了-讨论是关于强制方法重写的可能方法。我对发现有用的模式更感兴趣,这可能对使用Cloneable模式的确切情况有所帮助。
答案 0 :(得分:0)
这是对答案之一的阐述,建议使用typeid进行运行时检查: Forcing a derived class to overload a virtual method in a non-abstract base class
使用CRTP,可以提出以下基本思想:
创建类Cloneable<Derived>
,该类管理Derived
的克隆,并添加所有必需的运行时检查(似乎即使使用CRTP也无法进行编译时检查)。
但是,这并非不重要,而且还必须通过Cloneable
管理继承,如下所述:
#include <memory>
#include <cassert>
#include <type_traits>
#include <typeinfo>
class CloneableInterface {
public:
virtual std::unique_ptr<CloneableInterface> Clone() const = 0;
};
template <class... inherit_from>
struct InheritFrom : public inherit_from... {
};
template <class Derived, class AnotherBase = void, bool base_is_cloneable = std::is_base_of_v<CloneableInterface, AnotherBase>>
class Cloneable;
// three identical implementations, only the inheritance is different
// "no base is defined" case
template <class Derived>
class Cloneable<Derived, void, false> : public CloneableInterface {
public:
std::unique_ptr<CloneableInterface> Clone() const override {
assert(typeid(*this) == typeid(Derived));
return std::make_unique<Derived>(static_cast<const Derived&>(*this));
}
};
// Base is defined, and already provides CloneableInterface
template <class Derived, class AnotherBase>
class Cloneable<Derived, AnotherBase, true> : public AnotherBase {
...
};
// Base is defined, but has no CloneableInterface
template <class Derived, class AnotherBase>
class Cloneable<Derived, AnotherBase, false> : public AnotherBase, public CloneableInterface {
...
};
用法示例:
class Base : public Cloneable<Base> {
};
// Just some struct to test multiple inheritance
struct Other {
};
struct Derived : Cloneable<Derived, InheritFrom<Base, Other>> {
};
struct OtherBase {
};
struct OtherDerived : Cloneable<OtherDerived, InheritFrom<OtherBase>> {
};
int main() {
// compiles and runs
auto base_ptr = std::make_unique<Base>();
auto derived_ptr = std::make_unique<Derived>();
auto base_clone = base_ptr->Clone();
auto derived_clone = derived_ptr->Clone();
auto otherderived_ptr = std::make_unique<OtherDerived>();
auto otherderived_clone = otherderived_ptr->Clone();
}
欢迎任何批评家和改进建议!
答案 1 :(得分:0)
C ++ 17及更高版本提供了std::any
。从理论上讲,您可以制作一个克隆函数,而不是返回std::any*
:
struct A {
virtual std::any* Clone() const {
return new A(*this);
}
}
struct B : A {
int value;
// I suppose it doesn't have to be virtual here,
// but just in case we want to inherit the cloning capability from B as well
virtual std::any* Clone() const { // Note: you still need to override this function
return new B(*this); // in the lower levels, though
}
};
// Note: I'm still on MSVS2010, so this C++17 code is untested.
// Particularly problematic could be this main
int main() {
B b;
// Here is the clone
auto b_clone = std::any_cast<B*>(b.Clone());
delete b_clone;
}
再次,这是未经测试的,但在理论上应该可行。