如何实现继承已知类的未知类的副本

use*_*447 1 c++ inheritance

我需要一个不同类实例的数组(这里是:B),它们共享相同的基类(这里:A).
实例应存储为指针,因为它们在别处引用(此处未显示).
我这样试过:

class A {
public:
  A() {}
};

class B : public A {
  int m_magicValue;
public:
  B() : A() , m_magicValue(4711){}
  int getMagicValue() { return m_magicValue; }
};

class Manager {
  std::vector<A*> m_array;
public:
  Manager() {}
  virtual ~Manager() {
      for( size_t i=0; i<m_array.size(); i++ )
          delete m_array[i];
      m_array.clear();

  }
  void addA( const A& a ) {
    // here A is copied - here the problem occurs
    m_array.push_back( new A(a) );
//    createSomeReferences( m_array.back() );
  }
  A& getA( size_t idx ) {
    return *m_array[idx];
  }
};

int main() {
  B b;
  Manager manager;
  manager.addA(b);
  B& copiedB = (B&) manager.getA(0);
  int magic = copiedB.getMagicValue(); // magic is some random stuff instead of 4711!
  return 0;   
}
Run Code Online (Sandbox Code Playgroud)

管理器对B类一无所知,它只知道A.
有趣的事情发生在addA()中:这里尝试新的复制B并将指针存储在数组中.
但正如预期的那样它不能按预期工作;-) copiedB.getMagicValue(); 返回一个随机值.

原因相对清楚:使用A的复制构造函数而不是B的复制构造函数,因此只复制A部分.

所以现在我的问题是:有没有一种很好的方法来实现这样一个副本,它继承了整个类层次结构,即使只知道基类?

Ker*_* SB 5

您需要一个虚拟的"克隆"功能(有时通俗地称为"虚拟副本构造函数"):

struct A
{
    virtual ~A() { }
    virtual A * clone() const = 0;
};


struct B : A
{
    virtual A * clone() const
    {
        return new B(*this);
    }
};
Run Code Online (Sandbox Code Playgroud)

用法:

void addA(A const & a)
{
    insert(a.clone());
}
Run Code Online (Sandbox Code Playgroud)

但是,使用原始指针是非常愚蠢的,并且最好使克隆函数以及输入的容器std::unique_ptr<A>(并且代码在其他方面几乎完全相同).

(你现在可能会问自己,"为什么没有一个版本unique_ptr已经进行了深入的虚拟克隆,可能会被称为value_ptr<A>?这是一个很好的问题.)