473,327 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Problem copying objects.

Hi, imagine I have a simple hierarchy with three classes - 'Base',
'Derived1', and Derived2. Base is an absrtact class and the two Derived
classes are concrete. Imagine at some point in my code I have a pointer
p1 of type 'Base' which actually points to an instance of one of the
derived objects. I would like to create a new pointer p2, also of type
'Base', which points to a copy of whatever the first one pointed to.
That is, I would like to copy the object at p1 without knowing it's
exact type. Is that possible?

My first attempt was something like:

Base* p2 = new Base(*p1);

But this didn't make sense because i was trying to instanciate an
abstract class. Maybe i actually need to know the exact type? Another
approach I thought of was to give each class a virtual function called
'copy' which would duplicate the object and return a pointer to the
duplicate. Does this make sense or is there a better way?

Thanks for any help,

David

Jul 30 '06 #1
4 1391
es*****@googlemail.com wrote:
Hi, imagine I have a simple hierarchy with three classes - 'Base',
'Derived1', and Derived2. Base is an absrtact class and the two
Derived classes are concrete. Imagine at some point in my code I have
a pointer p1 of type 'Base' which actually points to an instance of
one of the derived objects. I would like to create a new pointer p2,
also of type 'Base', which points to a copy of whatever the first one
pointed to. That is, I would like to copy the object at p1 without
knowing it's exact type. Is that possible?
approach I thought of was to give each class a virtual function called
'copy' which would duplicate the object and return a pointer to the
duplicate. Does this make sense or is there a better way?
Exactly, this is known as a 'virtual constructor'. Personally, I
find the name a bit misleading, as it does not have anything to do with
making the _constructor_ of the object virtual (because this is
impossible). Instead, you use a regular virtual function, as you
described, which will return a copy of the appropriate type. This is how
it works:

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

class derived1
{
public
virtual derived1* clone () const { return new derived1; }
};

class derived2
{
public
virtual derived2* clone () const { return new derived2; }
};

int main ()
{
base* b1 = new derived1;
base* b2 = b1->clone ();
};

hth
--
jb

(reply address in rot13, unscramble first)
Jul 30 '06 #2

Jakob Bieling wrote:
es*****@googlemail.com wrote:
Hi, imagine I have a simple hierarchy with three classes - 'Base',
'Derived1', and Derived2. Base is an absrtact class and the two
Derived classes are concrete. Imagine at some point in my code I have
a pointer p1 of type 'Base' which actually points to an instance of
one of the derived objects. I would like to create a new pointer p2,
also of type 'Base', which points to a copy of whatever the first one
pointed to. That is, I would like to copy the object at p1 without
knowing it's exact type. Is that possible?
approach I thought of was to give each class a virtual function called
'copy' which would duplicate the object and return a pointer to the
duplicate. Does this make sense or is there a better way?

Exactly, this is known as a 'virtual constructor'. Personally, I
find the name a bit misleading, as it does not have anything to do with
making the _constructor_ of the object virtual (because this is
impossible). Instead, you use a regular virtual function, as you
described, which will return a copy of the appropriate type. This is how
it works:

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

class derived1
{
public
virtual derived1* clone () const { return new derived1; }
};

class derived2
{
public
virtual derived2* clone () const { return new derived2; }
};

int main ()
{
base* b1 = new derived1;
base* b2 = b1->clone ();
};

hth
--
jb

(reply address in rot13, unscramble first)
Great, thanks. I'll try that approach. I figured it would be a fairly
common problem and there would be some kind of design pattern for it
but didn't know what it was called. Thanks again.

David

Jul 30 '06 #3
Jakob Bieling schrieb:
class base
{
public:
virtual base* clone () const = 0;
};

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

You want to clone here, don't you?
};

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

int main ()
{
base* b1 = new derived1;
base* b2 = b1->clone ();
};

hth
--
Thomas
Jul 30 '06 #4
Thomas J. Gritzan <Ph*************@gmx.dewrote:
Jakob Bieling schrieb:
>class base
{
public:
virtual base* clone () const = 0;
};

class derived1
{
public
virtual derived1* clone () const { return new derived1; }

virtual derived1* clone () const { return new derived1(*this); }

You want to clone here, don't you?

Right, but I take it it's a matter of preference if you want to
create a new object by cloning the type (which I usually do) or by
cloning the actual object (which is what you are doing).

regards

--
jb

(reply address in rot13, unscramble first)
Jul 31 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: franky.backeljauw | last post by:
Hello, following my question on "std::copy versus pointer copy versus member copy", I had some doubts on the function memcpy, as was used by tom_usenet in his reply. - Is this a c++ standard...
6
by: ziwu | last post by:
std::copy() is a function from C++ library. Is memcpy() a function from C library, or is it re-implemented in C++ library? Why people say "In C++, don't use memcpy for non-POD types? What is...
6
by: solex | last post by:
Hello, I am trying to use serialization to copy objects. The object in question "Institution" inherits from a parent object "Party" both are marked as <Serializable()>. Initially I can copy an...
4
by: christof | last post by:
It'll be really easy...Sorry for that question: I've got a MasterPage <%@ Master Language="C#" ClassName="MasterP" %> and some slave.aspx.<%@ Page Language="C#" MasterPageFile="~/Master1.master"...
5
by: Eric | last post by:
I am implementing a variation on the Singleton design pattern, that allows up to 8 objects of a class to be instantiated, and returns a null pointer for anything more than 8. I am running into a...
11
by: Robert W. | last post by:
Here's some pseudo-code that describes what I'm trying to do: foreach(object in collection) { if (certain-condition) collection.Remove(object); } The problem with this is that foreach gets...
14
by: Frank | last post by:
Hello everyone, I am having trouble overloading the < operator for an assignment. I use a struct that contains information and I would like to sort this structure using STL sort with my own...
9
by: David | last post by:
Hi all, I posted my question two days ago, and tried to solve this problem. but until now I didn't solve that. and I cut my codes so maybe this time it is more readable. ...
20
by: ongaro.admin | last post by:
Hi, I'm experiencing a strange problem with .mdb files. We have two buildings connected by optical fiber (a single LAN). Everything works perfect with any file, any size, any application...
9
by: Julian | last post by:
Hi, I have a vector defined like this: std::vector<Load*LoadList; which is populated with different objects of classes that are derived from 'Load'. I need to make a copy of this list during...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.