我有几个类需要以下 clone
待定义函数:
struct Base
{
virtual Base * clone() const = 0;
};
struct A : public Base
{
Base * clone() const {
return new A(*this);
}
};
struct B : public Base
{
Base * clone() const {
return new B(*this);
}
};
struct X : public Base2
{
Base2 * clone() const {
return new X(*this);
}
};
我正在尝试使用 Cloneable mixin 来避免这种冗余代码:
template <typename BASE, typename TYPE>
class CloneableMixin
{
public:
BASE*clone() const {
return new TYPE( dynamic_cast<const TYPE &>(*this) );
}
};
struct A : public Base, public CloneableMixin<Base, A>
{
};
然而,这是行不通的,因为在new TYPE(*this)
来自 CloneableMixin
, *this
类型为 CloneableMixin<BASE, TYPE>
.
更新: CloneableMixin
可以dynamic_cast
到正确的类型。但现在我有另一个问题:CloneableMixin::clone
没有成功覆盖 Base::clone
,因此编译器报告 A 是抽象类型。
可以巧妙地使用virtual
吗?允许继承 CloneableMixin::clone
覆盖 Base::clone
?我应该为此使用一些宏吗?
您知道解决所有这些冗余代码的方法吗?
最佳答案
Can some clever use of virtual inheritance allow CloneableMixin::clone to override Base::clone?
你的 CloneableMixin<Base,Derived>
不能覆盖 Base
的任何方法- 任何一个
多态或隐藏 - 因为 CloneableMixin<Base,Derived>
是
不是来自 Base
.
另一方面,如果CloneableMixin<Base,Derived>
是源自Base
你将不再需要它是一个 mixin,因为 -
class Derived : public CloneableMixin<Base,Derived> {....};
会继承Base
.
因此,对于您的示例的需要,此处说明的解决方案就足够了:
#include <iostream>
// cloner v1.0
template <class Base, class Derived>
struct cloner : Base
{
Base *clone() const override {
return new Derived( dynamic_cast<const Derived &>(*this) );
}
~cloner() override {};
};
struct Base
{
virtual Base * clone() const = 0;
Base() {
std::cout << "Base()" << std::endl;
}
virtual ~Base() {
std::cout << "~Base()" << std::endl;
}
};
struct A : cloner<Base,A>
{
A() {
std::cout << "A()" << std::endl;
}
~A() override {
std::cout << "~A()" << std::endl;
}
};
int main()
{
A a;
Base * pb = a.clone();
delete pb;
}
(如果您正在编译为 C++03 标准而不是 C++11,那么您可能
只需删除出现的 override
关键字。)
这个解决方案将分解为一些更真实的类层次结构, 例如在这张 Template Method Pattern 的插图中:
#include <iostream>
#include <memory>
using namespace std;
// cloner v1.0
template<class B, class D>
struct cloner : B
{
B *clone() const override {
return new D(dynamic_cast<D const&>(*this));
}
~cloner() override {}
};
/* Abstract base class `abstract` keeps the state for all derivatives
and has some pure virtual methods. It has some non-default
constructors.
*/
struct abstract
{
virtual ~abstract() {
cout << "~abstract()" << endl;
}
int get_state() const {
return _state;
}
void run() {
cout << "abstract::run()" << endl;
a_root_method();
another_root_method();
}
virtual void a_root_method() = 0;
virtual void another_root_method() = 0;
virtual abstract * clone() const = 0;
protected:
abstract()
: _state(0) {
cout << "abstract(): state = " << get_state() << endl;
}
explicit abstract(int state) : _state(state) {
cout << "abstract(" << state << ") : state = "
<< get_state() << endl;
}
int _state;
};
/* Concrete class `concrete` inherits `abstract`
and implements the pure virtual methods.
It echoes the constructors of `abstract`. Since `concrete`
is concrete, it requires cloneability.
*/
struct concrete : cloner<abstract,concrete>
{
concrete() {
cout << "concrete(): state = " << get_state() << endl;
}
explicit concrete(int state) : abstract(state) { //<- Barf!
cout << "concrete(" << state << ") : state = "
<< get_state() << endl;
}
~concrete() override {
cout << "~concrete()" << endl;
}
void a_root_method() override {
++_state;
cout << "concrete::a_root_method() : state = "
<< get_state() << endl;
}
void another_root_method() override {
--_state;
cout << "concrete::another_root_method() : state = "
<< get_state() << endl;
}
};
int main(int argc, char **argv)
{
concrete c1;
unique_ptr<abstract> pr(new concrete(c1));
pr->a_root_method();
pr->another_root_method();
unique_ptr<abstract> pr1(pr->clone());
pr1->a_root_method();
return 0;
}
当我们尝试构建它时,编译器会在
初始化abstract(state)
在 concrete
的构造函数中(在 Barf!
评论),说:
error: type 'abstract' is not a direct or virtual base of 'concrete'
或类似的词。事实上,concrete
的直接基础不是 abstract
但是cloner<abstract,concrete>
.但是,我们不能将构造函数重写为:
/*Plan B*/ explicit concrete(int state) : cloner<abstract,concrete>(state){....}
因为没有这样的构造函数
cloner<abstract,concrete>::cloner<abstract,concrete>(int)
但是编译器的诊断建议修复。 这是是虚拟的
继承可以提供帮助。我们需要 abstract
成为concrete
的虚拟基地, 哪个
有效地意味着“concrete
的荣誉直接基地”,我们可以实现这一点
只需制作 B
cloner<B,D>
的虚拟 基地:
// cloner v1.1
template<class B, class D>
struct cloner : virtual B
{
B *clone() const override {
return new D(dynamic_cast<D const&>(*this));
}
~cloner() override {}
};
这样,我们就有了一个干净的构建和输出:
abstract(): state = 0
concrete(): state = 0
concrete::a_root_method() : state = 1
concrete::another_root_method() : state = 0
concrete::a_root_method() : state = 1
~concrete()
~abstract()
~concrete()
~abstract()
~concrete()
~abstract()
原则上有充分的理由警惕虚拟继承 并保留它的使用至少在它具有建筑学的情况下 基本原理 - 不是为了解决方法,因为我们刚才已经使用了它。
如果我们更愿意在没有虚拟继承的情况下解决这个问题,那么我们
必须以某种方式确保有 cloner<B,D>
的构造函数那
回显 B
的任何构造函数, 对于任意 B
.然后任意对应
D
的构造函数将能够初始化其直接基cloner<B,D>
无论争论是什么。
这是 C++03 的白日梦,但具有可变参数模板的魔力 C++11 中的参数很简单:
// cloner v1.2
template<class B, class D>
struct cloner : B
{
B *clone() const override {
return new D(dynamic_cast<D const&>(*this));
}
~cloner() override {}
// "All purpose constructor"
template<typename... Args>
explicit cloner(Args... args)
: B(args...){}
};
有了这个,我们可以重写 concrete
构造函数为 /*Plan B*/
, 和
我们再次拥有正确的构建和可执行文件。
关于C++ 可克隆混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10443637/