我有几个类需要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);
}
};
Run Code Online (Sandbox Code Playgroud)
我试图用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>
{
};
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用,因为在new TYPE(*this)from中CloneableMixin,*this属于类型CloneableMixin<BASE, TYPE>.
更新:在CloneableMixin能dynamic_cast以正确的类型.但是现在我有另一个问题:CloneableMixin::clone没有成功覆盖Base::clone,因此编译器报告A是抽象类型.
一些聪明的virtual继承使用是否允许CloneableMixin::clone覆盖Base::clone?我应该使用一些宏吗?
你知道绕过所有这些冗余代码的方法吗?
可以巧妙地使用虚拟继承允许CloneableMixin :: clone覆盖Base :: clone吗?
你CloneableMixin<Base,Derived>不能覆盖任何方法Base- 无论是多态还是隐藏 - 因为CloneableMixin<Base,Derived>不是从中派生出来的Base.
在另一方面,如果CloneableMixin<Base,Derived> 是来源于Base
你将不再有任何需要它是一个mixin,因为-
class Derived : public CloneableMixin<Base,Derived> {....};
Run Code Online (Sandbox Code Playgroud)
会继承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;
}
Run Code Online (Sandbox Code Playgroud)
(如果您正在编译C++ 03标准而不是C++ 11,那么您可以简单地删除override关键字的出现次数.)
这个解决方案将分解为一些更现实的类层次结构,例如在模板方法模式的插图中:
#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;
}
Run Code Online (Sandbox Code Playgroud)
当我们尝试构建它时,编译器将abstract(state)在concrete(在Barf!
注释处)的构造函数中初始化时给出错误,说:
error: type 'abstract' is not a direct or virtual base of 'concrete'
Run Code Online (Sandbox Code Playgroud)
或者说是这样的话.事实上,直接基础concrete是不是abstract
,但cloner<abstract,concrete>.但是,我们不能将构造函数重写为:
/*Plan B*/ explicit concrete(int state) : cloner<abstract,concrete>(state){....}
Run Code Online (Sandbox Code Playgroud)
因为没有这样的构造函数
cloner<abstract,concrete>::cloner<abstract,concrete>(int)
Run Code Online (Sandbox Code Playgroud)
但编译器的诊断建议修复.这是虚拟继承可以帮助.我们需要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 {}
};
Run Code Online (Sandbox Code Playgroud)
有了它,我们有一个干净的构建和输出:
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()
Run Code Online (Sandbox Code Playgroud)
有充分的理由在原则上对虚拟继承保持警惕并至少保留其用于具有架构原理的情况 - 而不是用于解决方法,正如我们刚才使用的那样.
如果我们不喜欢这个问题,虚拟继承的事,那么我们必须设法确保有一个构造函数的cloner<B,D>呼应任何的constuctor B,任意B.然后任何相应的构造函数D都能够初始化它的直接基数,cloner<B,D>
无论参数是什么.
这是C++ 03的一个pipedream,但是在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...){}
};
Run Code Online (Sandbox Code Playgroud)
有了这个,我们可以重写concrete构造函数/*Plan B*/,并且我们再次拥有正确的构建和可执行文件.
在 Cloneable mixin 实例化期间,派生类仍处于不完整类型。您可以尝试添加众所周知的额外间接级别,如下所示:
template
<
typename Derived
>
class Cloneable
:
private CloneableBase
{
public:
Derived* clone() const
{
return static_cast<Derived*>(this->do_clone());
}
private:
virtual Cloneable* do_clone() const
{
return new Derived(static_cast<const Derived&>(*this));
}
};
class CloneableBase
{
public:
CloneableBase* clone() const
{
return do_clone();
}
private:
virtual CloneableBase* do_clone() const=0;
};
class MyClass: public Cloneable<MyClass>;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1644 次 |
| 最近记录: |