6

我有几个类需要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>
{
};

但是,这不起作用,因为 in new TYPE(*this)from CloneableMixin,*this是 type CloneableMixin<BASE, TYPE>

更新:CloneableMixin可以dynamic_cast正确的类型。但是现在我有另一个问题:CloneableMixin::clone没有成功覆盖Base::clone,因此编译器报告 A 是抽象类型。

可以巧妙地使用virtual继承允许CloneableMixin::clone覆盖Base::clone吗?我应该为此使用一些宏吗?

你知道解决所有这些冗余代码的方法吗?

4

2 回答 2

6

虚拟继承的一些巧妙使用可以允许 CloneableMixin::clone 覆盖 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关键字的出现。)

该解决方案将分解为一些更真实的类层次结构,例如在模板方法模式的插图中:

#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)的构造函数的初始化时给出错误 ,说:concreteBarf!

error: type 'abstract' is not a direct or virtual base of 'concrete'

或大意的话。确实,concreteis not abstract but的直接基数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>相呼应。然后任何相应的构造函数将能够初始化它的直接基 ,无论参数是什么。BBDcloner<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*/,并且我们再次获得了正确的构建和可执行文件。

于 2013-02-26T13:31:05.233 回答
0

在 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>;
于 2012-05-04T06:45:44.847 回答