473,395 Members | 2,437 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,395 software developers and data experts.

Copying Polymorphically

I want to make a copy of an object that is passed to me
via a base class pointer. No slicing allowed, so the copy
has to be the same dynamic type as the original. How do I
do this?

base_class * clone(const base_class * orig)
{
base_class * p;

... // A miracle occurs (HOW?)

// And now p points to a copy of *orig,
// with the same dynamic type as *orig.

return p;
}

Now I want to be able to do this:

class base_class { ... };
class derived_class : public base_class { ... };

derived_class x;
base_class * newxp = clone(&x);
// Now newxp points to a copy of x.
// The copy has type derived_class.

Jul 23 '05 #1
5 1041
"Matt Bitten" <mb*******@yahoo.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
I want to make a copy of an object that is passed to me
via a base class pointer. No slicing allowed, so the copy
has to be the same dynamic type as the original. How do I
do this?

base_class * clone(const base_class * orig)
{
base_class * p;

... // A miracle occurs (HOW?)

// And now p points to a copy of *orig,
// with the same dynamic type as *orig.

return p;
}

Now I want to be able to do this:

class base_class { ... };
class derived_class : public base_class { ... };

derived_class x;
base_class * newxp = clone(&x);
// Now newxp points to a copy of x.
// The copy has type derived_class.


Make your clone function a virtual member and override it in each derived
class. Each clone function returns an instance of the class it belongs to.

DW

Jul 23 '05 #2
Matt Bitten wrote:
I want to make a copy of an object that is passed to me
via a base class pointer. No slicing allowed, so the copy
has to be the same dynamic type as the original. How do I
do this?

base_class * clone(const base_class * orig)
{
base_class * p;

... // A miracle occurs (HOW?)

// And now p points to a copy of *orig,
// with the same dynamic type as *orig.

return p;
}

Now I want to be able to do this:

class base_class { ... };
class derived_class : public base_class { ... };

derived_class x;
base_class * newxp = clone(&x);
// Now newxp points to a copy of x.
// The copy has type derived_class.


Make clone() a pure virtual member function of base_class:

class base_class
{
public:
virtual base_class* clone() const = 0;
//...
};

And add an implementation to derived_class:

class derived_class
{
public:
virtual derived_class* clone() const
{
return new derived_class(*this);
}
//...
};

Jul 23 '05 #3
--- Rolf Magnus wrote:
clone() a pure virtual member function of base_class:

[etc]

Thanks!

By the way, just for curiousity's sake, is it *possible* to do
this without a virtual member function? I'm guessing it is
not.

Jul 23 '05 #4
"Matt Bitten" <mb*******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
--- Rolf Magnus wrote:
clone() a pure virtual member function of base_class:

[etc]

Thanks!

By the way, just for curiousity's sake, is it *possible* to do
this without a virtual member function? I'm guessing it is
not.


Well, it's sort of possible, but the solution is truly horrible:

Car *pcar = dynamic_cast<Car*>(&vehicle);
if (pcar)
// copy it
else
{
Bus *pbus = dynamic_cast<Bus*>(&vehicle);
if (pbus)
// copy it
else
{
// ad nauseum

This sort of code is a sure sign of bad design.

However, be very careful with virtual clone functions. You can easily return
the wrong type if you are not careful. This is likely to cause hard to find
bugs.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 23 '05 #5
"Matt Bitten" <mb*******@yahoo.com> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
By the way, just for curiousity's sake, is it *possible* to do
this without a virtual member function? I'm guessing it is
not.


If you go to enough trouble you can do just about anything another way. The
first C++ compiler produced C code, which was then compiled by a C compiler,
so anything you can do in C++ can be done in C. Nothing could be more
suitable to achieve with virtual functions than clone functions. You could
do it another way if you were determined to, but you couldn't do it better.

DW
Jul 23 '05 #6

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

Similar topics

5
by: Thomas Lotze | last post by:
Hi, another question: What's the most efficient way of copying data between two file-like objects? f1.write(f2.read()) doesn't seem to me as efficient as it might be, as a string containing...
3
by: Robert Tarantino | last post by:
Hello, I am trying to find a way to create a scheduled task or service that will copy my local profile folders under "Documents and settings" to a network drive. This would allow me to restore...
4
by: Alex Vinokur | last post by:
Copying files : input to output =============================== C/C++ Performance Tests ======================= Using C/C++ Program Perfometer http://sourceforge.net/projects/cpp-perfometer...
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...
5
by: Martin Magnusson | last post by:
Hi! I have a class with a private member which is a pointer to an abstract class, which looks something like this: class Agent { public: void Step( Base* newB ); private:
22
by: Matt | last post by:
When browsing a web page a user has the ability to highlight content on a page (by holding down the left mouse button and dragging the mouse over the desired content). Is there a way to disable...
0
by: Dan | last post by:
We debug a VB.Net App that uses compact framework to a DAP Windows CE.Net device. When I debug I would like to only have the EXE copied down. Right now it copies any referrences as well and...
0
by: surya | last post by:
Hi, I need to enable shadow copying network share folder assemblies. Currently if the path is given as the network share folder path, shadow copying is not enabled. Should something special be...
1
by: Mark Smith | last post by:
I'm trying to copy data from a 1D array to a 2D array. The obvious thing doesn't work: int twoDee = new int; int oneDee = new int { 1, 2 }; Array.Copy(oneDee, 2, twoDee, 2, 2); This causes a...
1
by: RonLandreth | last post by:
I am writing an accounting system for a class I'm taking at SLU. I need help figuring out the best way to go about copying a 2D array to a temporary 2D array, for eventually copying it back. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.