Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a derived C++ class clone itself via a base pointer?

Tags:

c++

cloning

Here's what I'm trying to do (this code doesn't work):

class Base
{
    virtual Base *clone() { return new Base(this); }
    virtual void ID() { printf("BASE");
};

class Derived : publc Base
{
    virtual Base *clone() { return new Derived(this); }
    virtual void ID() { printf("DERIVED"); }
}

.
.
Derived d;
Base *bp = &d;
Base *bp2 = bp->clone();

bp2->ID();

What I'd like is to see "DERIVED" printed out... what I get is "BASE". I'm a long-time C programmer, and fairly experienced with C++... but I'm not making any headway with this one... any help would be appreciated.

like image 937
wanlessv Avatar asked Jun 28 '10 22:06

wanlessv


2 Answers

Once all the compile errors are fixed, I ended up with this:

#include <cstdio>

class Base
{
  public:
    Base() {}
    Base(const Base&) {}
    virtual Base *clone() { return new Base(*this); }
    virtual void ID() { printf("BASE"); }
};

class Derived : public Base
{
  public:
    Derived() {}
    Derived(const Derived&) {}
    virtual Base *clone() { return new Derived(*this); }
    virtual void ID() { printf("DERIVED"); }
};


int main()
{
  Derived d;
  Base *bp = &d;
  Base *bp2 = bp->clone();

  bp2->ID();
}

Which gives you what you are looking for -- DERIVED.

like image 146
Fred Larson Avatar answered Sep 21 '22 07:09

Fred Larson


That code is riddled with syntactical errors. Perhaps most significantly, Derived doesn't inherit from Base. Secondly, aside from the syntactical errors (probably simple typos), Base obviously needs a virtual destructor. The clone method pretty much demands that you can call operator delete on a base pointer (Base*).

class Base
{
public:
    virtual ~Base() {}
    virtual Base* clone() const { return new Base(*this); }
    virtual void ID() const { printf("BASE"); }
};

class Derived: public Base
{
public:
    // [Edit] Changed return type to Derived* instead of Base*.
    // Thanks to Matthieu for pointing this out. @see comments below.
    virtual Derived* clone() const { return new Derived(*this); }
    virtual void ID() const { printf("DERIVED"); }
};

int main()
{
    Derived d;
    Base* bp = &d;

    Base* bp2 = bp->clone();
    bp2->ID(); // outputs DERIVED as expected
    delete bp2;
}
like image 36
stinky472 Avatar answered Sep 21 '22 07:09

stinky472