Duplicating objects with a common base class

Discussion in 'C++' started by Pete Nudd, Apr 15, 2004.

  1. Pete Nudd

    Pete Nudd Guest

    I have a system in which potentialy 100's of classes derive directly
    from a common base class. I will often have a pointer of the base class
    type which points to an object of a derived class. Now the hard bit: I
    need to be able to create a copy of the object.

    I know I could write a virtual Duplicate() function in the base class,
    and then override it in each of the derived classes, but that seems very
    time consuming when the code is going to look more or less identical in
    each case.


    Any Ideas?

    TIA

    Pete Nudd
     
    Pete Nudd, Apr 15, 2004
    #1
    1. Advertising

  2. "Pete Nudd" <> wrote in message
    news:...
    > I have a system in which potentialy 100's of classes derive directly
    > from a common base class. I will often have a pointer of the base class
    > type which points to an object of a derived class. Now the hard bit: I
    > need to be able to create a copy of the object.
    >
    > I know I could write a virtual Duplicate() function in the base class,
    > and then override it in each of the derived classes, but that seems very
    > time consuming when the code is going to look more or less identical in
    > each case.
    >
    >
    > Any Ideas?
    >
    > TIA
    >
    > Pete Nudd


    You're doing it the right way, there isn't a better way in C++. Some people
    would cut out some of the repetition by using a macro but personally I
    wouldn't recommend it. Its only a one line function so it not too hard to
    write it each time.

    john

    BTW the traditional name for the function you are calling Duplicate is
    Clone.
     
    John Harrison, Apr 15, 2004
    #2
    1. Advertising

  3. Pete Nudd

    Derek Guest

    Pete Nudd wrote:
    > I have a system in which potentialy 100's of classes
    > derive directly from a common base class. I will often
    > have a pointer of the base class type which points to an
    > object of a derived class. Now the hard bit: I need to be
    > able to create a copy of the object.
    >
    > I know I could write a virtual Duplicate() function in
    > the base class, and then override it in each of the
    > derived classes, but that seems very time consuming when
    > the code is going to look more or less identical in each
    > case.
    >
    > Any Ideas?


    You could use a template to supply a clone() function.
    (I think -- you might want a second opinion because I'm
    making this up as I go along.) Say your base is:

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

    Create a template that defines clone() for any type T
    and is itself derived from Base:

    template<class T>
    class CloneableBase : public Base
    {
    public:
    Base* clone() const
    {
    return new T(dynamic_cast<const T&>(*this));
    }
    };

    Now inherit from Cloneable instead of using Base
    directly:

    class Foo : public CloneableBase<Foo> {};
    class Bar : public CloneableBase<Bar> {};

    And you can write:

    Base* pb1 = new Foo;
    Base* pb2 = pb1->clone();
     
    Derek, Apr 15, 2004
    #3
  4. Derek wrote:
    >
    > Pete Nudd wrote:

    [snip]
    >
    > You could use a template to supply a clone() function.
    > (I think -- you might want a second opinion because I'm
    > making this up as I go along.) Say your base is:
    >
    > class Base
    > {
    > public:
    > virtual Base* clone() const = 0;
    > virtual ~Base() {}
    > };
    >
    > Create a template that defines clone() for any type T
    > and is itself derived from Base:
    >
    > template<class T>
    > class CloneableBase : public Base
    > {
    > public:
    > Base* clone() const
    > {
    > return new T(dynamic_cast<const T&>(*this));
    > }
    > };
    >
    > Now inherit from Cloneable instead of using Base
    > directly:
    >
    > class Foo : public CloneableBase<Foo> {};
    > class Bar : public CloneableBase<Bar> {};
    >
    > And you can write:
    >
    > Base* pb1 = new Foo;
    > Base* pb2 = pb1->clone();


    Nitpicking: I'd disagree with the use of dynamic_cast here (other than
    for debugging). If CloneableBase<T> is used as intended:
    class Bar : public CloneableBase<Bar> {};

    then the result of dynamic_cast is equivalent to static_cast.
    The only time its observed behaviour will differ from that of static_cast
    is when someone grossly misuses CloneableBase<T>, e.g.
    class Bar : public CloneableBase<Foo> {};

    This, however, should be detected at compile time and, at any rate, never
    happen in the production code, so static_cast will do. If this still
    didn't sound safe enough, I'd just revert to the original scheme.

    (FWIW, I use dynamic_cast only when I really have to (i.e. when downcasting
    can only be resolved at run-time and must be controlled for safety, and
    there are no design alternatives). The former use is a domain serviced
    well enough by virtual functions alone. On a practical note, at least
    some popular implementations of dynamic_cast are very inefficient).

    Denis
     
    Denis Remezov, Apr 16, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Markus Bertheau

    Multiple inheritance with a common base class

    Markus Bertheau, Aug 10, 2004, in forum: Python
    Replies:
    0
    Views:
    321
    Markus Bertheau
    Aug 10, 2004
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    557
    John Carson
    Sep 3, 2005
  3. Hicham Mouline
    Replies:
    1
    Views:
    603
    Victor Bazarov
    Apr 20, 2009
  4. Karan Rajput
    Replies:
    2
    Views:
    150
    Abinoam Jr.
    Dec 22, 2010
  5. Christopher Benson-Manica

    Duplicating objects

    Christopher Benson-Manica, Dec 6, 2005, in forum: Javascript
    Replies:
    15
    Views:
    198
    Christopher Benson-Manica
    Dec 8, 2005
Loading...

Share This Page