C++ Calling a copy constructor on an unknown derived class through an abstract base class

巧了我就是萌 提交于 2019-11-30 08:25:31

问题


I'm making a tree that has several different node types: a binary node, a unary node, and a terminal node. I've got an ABC that all the nodes inherit from. I'm trying to write a recursive copy constructor for the tree like so:

class gpnode
{
public:
  gpnode() {};
  virtual ~gpnode() {};
  gpnode(const gpnode& src) {};

  gpnode* parent;
}

class bnode:gpnode
{
public:
  bnode() {//stuff};
  ~bnode() {//recursive delete};

  bnode(const bnode& src)
  {
    lnode = gpnode(src.lnode);
    rnode = gpnode(src.rnode);

    lnode->parent = this;
    rnode->parent = this;
  }

  gpnode* lnode;
  gpnode* rnode;
}

class unode:gpnode
{
public:
  unode() {//stuff};
  ~unode() {//recursive delete};

  unode(const unode& src)
  {
    node = gpnode(src.node);

    node->parent = this;
  }

  gpnode* node;
}

My problem is that I can't do

node = gpnode(src.node);

because gpnode is a virtual class. I could do

node = unode(src.node);

but that doesn't work when the child of a unode is a bnode. How do I get it to intelligently call the copy constructor I need it to?


回答1:


You need to implement cloning.

   class base
   {
   public:
       virtual base* clone() const = 0;
   }

   class derived : public base
   {
   public:
       derived(){}; // default ctor
       derived(const derived&){}; // copy ctor

       virtual derived* clone() const { return new derived(*this); };
   };

Etceteras




回答2:


To do this you have to provide a clone-method for your objects, that returns a pointer of the appropriate type. If all your classes have copy-constructors, that is as simple as that:

node* clone() const {
    return new node(*this);
}

Where node is the class you are writing the clone-method for. You would of course have to declare that method in your base-class:

virtual gpnode* clone() const = 0;



回答3:


Use virtual constructor.



来源:https://stackoverflow.com/questions/7912994/c-calling-a-copy-constructor-on-an-unknown-derived-class-through-an-abstract-b

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!