Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Copy Class from Base

Reply
Thread Tools

Copy Class from Base

 
 
miben@miben.net
Guest
Posts: n/a
 
      11-25-2005
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.

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-25-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Base class method Need base class value Karan Rajput Ruby 2 12-22-2010 04:47 PM
base class public type (non template and template base class) Hicham Mouline C++ 1 04-20-2009 03:28 PM
Copy construction with inaccessible base class copy c-tor Victor Bazarov C++ 15 03-04-2007 03:47 PM
Access of base class' private base class: qualification required, why Alf P. Steinbach C++ 6 09-03-2005 04:03 PM
Re: Copy assignment for derived class when base class has privatemembers Karl Heinz Buchegger C++ 3 08-06-2003 11:44 AM



Advertisments