Copy Class from Base

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • miben@miben.net

    #1

    Copy Class from Base

    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.

  • Victor Bazarov

    #2
    Re: Copy Class from Base

    miben@miben.net wrote:[color=blue]
    > 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
    > }
    > }[/color]
    ;
    [color=blue]
    >
    > class InheritsAgain: public Base {
    > public:
    > void operator =(const InheritsAgain &ia) {
    > // copy items
    > }
    > }[/color]
    ;
    [color=blue]
    >
    > int main(void) {
    > Inherits inherits;
    > InheritsAgain ia;
    >
    > Add(inherits);
    > Add(ia);[/color]

    What do those do? You haven't declared any 'Add' function.
    [color=blue]
    > }
    >
    > Base &CreateNewItem( const &Base base) {
    > Base *lpbase;
    >
    > lpbase = new Base;[/color]

    Consider definiing and initialising in one statement instead of two:

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

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

    Base *lpbase = new Base(base);
    [color=blue]
    >
    > return lpbase;[/color]

    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.
    [color=blue]
    > }
    >
    > 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?[/color]

    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.
    [color=blue]
    > 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.[/color]

    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


    Comment

    Working...