【问题标题】:C++ Cloneable mixinC++ 可克隆混合
【发布时间】:2012-05-04 06:15:58
【问题描述】:

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

但是,这不起作用,因为在来自 CloneableMixinnew TYPE(*this) 中,*this 的类型为 CloneableMixin&lt;BASE, TYPE&gt;

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

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

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

【问题讨论】:

  • Base::clone 必须是 virtual 才能使 =0 有效。
  • @rhalbersma 我不确定是否完全一样。并非我所有的可克隆类都具有相同的基本类型(例如BaseBase2)。
  • @rhalbersma 谢谢,很好。

标签: c++ clone mixins


【解决方案1】:

能否巧妙地使用虚拟继承让 CloneableMixin::clone 覆盖 Base::clone?

您的 CloneableMixin&lt;Base,Derived&gt; 不能覆盖 Base 的任何方法 - 要么 多态或隐藏 - 因为CloneableMixin&lt;Base,Derived&gt; 是 不是源自Base

另一方面,如果CloneableMixin&lt;Base,Derived&gt; 派生自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;
}

当我们尝试构建它时,编译器会在 concrete 的构造函数中的初始化 abstract(state)(在 Barf! 评论),说:

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

或类似的词。确实,concrete 的直接基数不是abstract 但是cloner&lt;abstract,concrete&gt;。但是,我们不能将构造函数重写为:

/*Plan B*/ explicit concrete(int state) : cloner<abstract,concrete>(state){....}

因为没有像这样的构造函数

cloner<abstract,concrete>::cloner<abstract,concrete>(int)

但是编译器的诊断建议修复。 这个是虚拟的 继承可以提供帮助。我们需要abstract 成为concrete虚拟基础,这 实际上意味着“concrete的名誉直属基地”,我们可以做到这一点 只需将B 设为cloner&lt;B,D&gt;虚拟 基础:

// 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&lt;B,D&gt; 的构造函数 回显Bany 构造函数,用于任意B。那么任何对应的 D 的构造函数将能够初始化其直接基 cloner&lt;B,D&gt; 无论论据是什么。

这是 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*/,并且 再次,我们有一个正确的构建和可执行文件。

【讨论】:

  • P.S.使用 GCC 4.7.2 和 Clang 3.2 进行编译
【解决方案2】:

在您的 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>;

【讨论】:

  • 这个解决方案对我不起作用。就我而言,A virtual A* clone() const = 0; 并且 B 做类似的事情。当我让 C 时,GCC 说 undefined reference to 'vtable for C'。如果我手动实现它(不使用模板),代码可以正常工作。 [GCC 4.5.3,Cygwin]
  • @AsukaKenji-SiuChingPong- 您能否提供一个 LiveWorkspace 示例来展示您的代码?
猜你喜欢
  • 1970-01-01
  • 2015-03-27
  • 1970-01-01
  • 2011-02-08
  • 2023-03-27
  • 1970-01-01
  • 2011-03-29
  • 1970-01-01
  • 2012-05-06
相关资源
最近更新 更多