Copy Class from Base

Discussion in 'C++' started by miben@miben.net, Nov 25, 2005.

  1. Guest

    I want to create a new inherited class givin its base. For example:

    class Base {
    public:
    void operator =(const Base &base) {
    // copy base items
    }
    };

    class Inherits: public Base {
    public:
    void operator =(const Inherits &inherits) {
    // copy items
    }
    }

    class InheritsAgain: public Base {
    public:
    void operator =(const InheritsAgain &ia) {
    // copy items
    }
    }

    int main(void) {
    Inherits inherits;
    InheritsAgain ia;

    Add(inherits);
    Add(ia);
    }

    Base &CreateNewItem(const &Base base) {
    Base *lpbase;

    lpbase = new Base;
    *lpbase = const_cast<Base &>(base); // Copy new item

    return lpbase;
    }

    I need to fire the Inherits copy function but it keeps wanting to fire
    the Base copy function. Is there a better way to do this? I would
    prefer the CreateNewItem function to handle the 'new' and 'delete'
    operations of these new items. I need a generic function to do this
    for all types of classes made from Base.
    , Nov 25, 2005
    #1
    1. Advertising

  2. wrote:
    > I want to create a new inherited class givin its base. For example:
    >
    > class Base {
    > public:
    > void operator =(const Base &base) {
    > // copy base items
    > }
    > };
    >
    > class Inherits: public Base {
    > public:
    > void operator =(const Inherits &inherits) {
    > // copy items
    > }
    > }

    ;

    >
    > class InheritsAgain: public Base {
    > public:
    > void operator =(const InheritsAgain &ia) {
    > // copy items
    > }
    > }

    ;

    >
    > int main(void) {
    > Inherits inherits;
    > InheritsAgain ia;
    >
    > Add(inherits);
    > Add(ia);


    What do those do? You haven't declared any 'Add' function.

    > }
    >
    > Base &CreateNewItem(const &Base base) {
    > Base *lpbase;
    >
    > lpbase = new Base;


    Consider definiing and initialising in one statement instead of two:

    Base *lpbase = new Base;

    > *lpbase = const_cast<Base &>(base); // Copy new item


    What's the point of this? Couldn't you copy-construct? Like this:

    Base *lpbase = new Base(base);

    >
    > return lpbase;


    Oh, and the entire body of your function could be a single return:

    return new Base(base);

    and no need for any casts or any local variables.

    > }
    >
    > I need to fire the Inherits copy function but it keeps wanting to fire
    > the Base copy function. Is there a better way to do this?


    To do exactly what? Have you tried declaring the assignment operator
    'virtual'? It probably won't help you, though. What you need is the
    'clone' method. A virtual function that creates a perfect copy of the
    object for which it's called.

    > I would
    > prefer the CreateNewItem function to handle the 'new' and 'delete'
    > operations of these new items. I need a generic function to do this
    > for all types of classes made from Base.


    class Base {
    public:
    virtual Base* clone() const {
    return new Base(*this);
    }
    };

    class Inherits: public Base {
    public:
    virtual Base* clone() const {
    return new Inherits(*this);
    }
    };

    class InheritsAgain: public Base {
    public:
    virtual Base* clone() const {
    return new InheritsAgain(*this);
    }
    };


    V
    Victor Bazarov, Nov 25, 2005
    #2
    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. Karl Heinz Buchegger
    Replies:
    3
    Views:
    522
    Karl Heinz Buchegger
    Aug 6, 2003
  2. Alf P. Steinbach
    Replies:
    6
    Views:
    531
    John Carson
    Sep 3, 2005
  3. Victor Bazarov
    Replies:
    15
    Views:
    750
    Greg Comeau
    Mar 4, 2007
  4. Hicham Mouline
    Replies:
    1
    Views:
    579
    Victor Bazarov
    Apr 20, 2009
  5. Karan Rajput
    Replies:
    2
    Views:
    129
    Abinoam Jr.
    Dec 22, 2010
Loading...

Share This Page