Copy Class from Base

M

miben

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.
 
V

Victor Bazarov

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top