9

假设我有一个克隆派生类的基类:

class Base
{
    public:
        virtual Base * clone()
        {
            return new Base();
        }

        // ...
};

我有一组派生类,它们使用奇怪的重复模板模式实现:

template <class T>
class CRTP : public Base
{
    public:
        virtual T * clone()
        {
            return new T();
        }

        // ...
};

我试图从中进一步得出这样的结论:

class Derived : public CRTP<Derived>
{
    public:
        // ...
};

我收到编译错误,效果如下:

error C2555: 'CRTP<T>::clone': overriding virtual function return type differs and is not covariant from 'Base::clone'

我意识到这可能是编译器在实例化 CRTP 时不完全了解 Derived 的继承树的结果。此外,将返回类型 (T*) 替换为 (Base*) 也可以编译。但是,我想知道是否有一种保留上述语义的解决方法。

4

2 回答 2

3

一个不那么漂亮的解决方法。

class Base
{
    protected:
        virtual Base * clone_p()
        {
            return new Base();
        }
};


template <class T>
class CRTP : public Base
{
    protected:
        virtual CRTP* clone_p()
        {
            return new T;
        }
    public:
        T* clone()
        {
            CRTP* res = clone_p();
            return static_cast<T*>(res);
        }
};


class Derived : public CRTP<Derived>
{
    public:
};

如果您觉得它更安全,请使用dynamic_cast<>而不是。static

于 2013-06-19T21:49:10.507 回答
1

如果您可以忍受必须使用不同的语法来指定完整类型,则可以执行以下操作(警告:未经测试的代码):

让我们首先从机器开始:

// this gives the complete type which needs to be used to create objects
// and provides the implementation of clone()
template<typename T> class Cloneable:
  public T
{
public:
  template<typename... U> Cloneable(U&&... u): T(std::forward<U>(u) ...) {}
  T* clone() { return new Cloneable(*this); }
private:
  // this makes the class complete
  // Note: T:: to make it type dependent, so it can be found despite not yet defined
  typename T::CloneableBase::CloneableKey unlock() {}
};

// this provides the clone function prototype and also makes sure that only
// Cloneable<T> can be instantiated
class CloneableBase
{
  template<typename T> friend class Cloneable;

  // this type is only accessible to Clonerable instances
  struct CloneableKey {};

  // this has to be implemented to complete the class; only Cloneable instances can do that
  virtual CloneableKey unlock() = 0;
public:
  virtual CloneableBase* clone() = 0;
  virtual ~CloneableBase() {}
};

好的,现在是实际的类层次结构。那个很标准;没有 CRTP 中间体或其他并发症。然而,没有类实现该clone函数,但都继承了声明(直接或间接)CloneableBase

// Base inherits clone() from CloneableBase
class Base:
  public CloneableBase
{
  // ...
};

// Derived can inherit normally from Base, nothing special here
class Derived:
  public Base
{
  // ...
};

以下是创建对象的方法:

// However, to create new instances, we actually need to use Cloneable<Derived>
Cloneable<Derived> someObject;
Derived* ptr = new Cloneable<Derived>(whatever);

// Now we clone the objects
Derived* clone1 = someObject.clone();
Derived* clone2 = ptr->clone();

// we can get rid og the objects the usual way:
delete ptr;
delete clone1;
delete clone2;

请注意,a Cloneable<Derived>is-a Derived(它是一个子类),因此您Cloneable只需要用于构造,并且可以假装使用Derived对象(好吧,tyepinfo也将其标识为Cloneable<Derived>)。

于 2013-06-19T22:03:18.357 回答