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 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)
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
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
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) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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...
|
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...
|
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"...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |