5

let's say I have a pointer to some base class and I want to create a new instance of this object's derived class. How can I do this?

class Base
{
    // virtual
};

class Derived : Base
{
    // ...
};


void someFunction(Base *b)
{
    Base *newInstance = new Derived(); // but here I don't know how I can get the Derived class type from *b
}

void test()
{
    Derived *d = new Derived();
    someFunction(d);
}
Xeo
  • 129,499
  • 52
  • 291
  • 397
Ben
  • 4,486
  • 6
  • 33
  • 48
  • 2
    Can you please fix the syntax of the presented code, and be a bit more clear about what you're asking. – Cheers and hth. - Alf Jul 08 '11 at 14:51
  • 2
    @Alf: Other than the `...` and missing semicolons, what's wrong? – Lightness Races in Orbit Jul 08 '11 at 14:55
  • What are you really trying to do here? – Mark B Jul 08 '11 at 15:03
  • oh, I'm sorry... it's true I started using stackoverflow a long time ago, but I don't use it very often, so I am not familiar with the rules how to use it. Just had to look at the FAQ to find out how to accept an answer... it's not very eye-popping. – Ben Jul 12 '11 at 08:40
  • @Ben: It's stated pretty clearly under "How do I ask questions here?" – Lightness Races in Orbit Jul 12 '11 at 08:59
  • @Tomalak: yes.. but you'd have to read that first ;) And I didn't until today, because just asking a question is pretty intuitive and doesn't need any explanation. Please forgive my misbehaviour! I promise to improve on that :) – Ben Jul 12 '11 at 12:05

2 Answers2

14

Cloning

struct Base {
   virtual Base* clone() { return new Base(*this); }
};

struct Derived : Base {
   virtual Base* clone() { return new Derived(*this); }
};


void someFunction(Base* b) {
   Base* newInstance = b->clone();
}

int main() {
   Derived* d = new Derived();
   someFunction(d);
}

This is a pretty typical pattern.


Creating new objects

struct Base {
   virtual Base* create_blank() { return new Base; }
};

struct Derived : Base {
   virtual Base* create_blank() { return new Derived; }
};


void someFunction(Base* b) {
   Base* newInstance = b->create_blank();
}

int main() {
   Derived* d = new Derived();
   someFunction(d);
}

Though I don't think that this a typical thing to do; it looks to me like a bit of a code smell. Are you sure that you need it?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • ok... this looks like it could help me. But I don't want to copy the object, I just want to create a new instance with the derived class's standard constructor. – Ben Jul 08 '11 at 15:00
  • erm... thanks for your edit :) I could have guessed that myself ;) Actually, I'm not sure at all if my architecture is good. I just don't know how to do it better. Is there any good (and modern) C++ programming book you could recommend where I can learn about programming patterns? – Ben Jul 08 '11 at 15:06
  • @Ben: [Here](http://jcatki.no-ip.org/fncpp/Resources) are some recommended resources. – Lightness Races in Orbit Jul 08 '11 at 15:07
  • I ordered the (More) Effective C++ books. I hope it will help me to create a better design... – Ben Jul 12 '11 at 08:43
6

It's called clone and you implement a virtual function that returns a pointer to a dynamically-allocated copy of the object.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Puppy
  • 144,682
  • 38
  • 256
  • 465