473,396 Members | 1,784 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,396 software developers and data experts.

How to copy derived class from a base pointer

This is what I have

class base{public:virtual ~base();};
class derived1 : public base {};
class derived2 : public base {};

class A
{
public:
A(base *b_):b(b_) {
}
A(const A&a)
{
//What to do here *************
}
private:
base *b;
};

I'm not sure how to write the copy constructor to make sure it makes
the right copy of b.
How would the copy constructor know which derived type to copy?

Mar 18 '06 #1
9 2334
in the class base{public:virtual ~base();};
you should add virtual clone() = 0;
the things will be OK!
cheer!

Mar 18 '06 #2
add a member function - baseclass copy constructor

Mar 18 '06 #3

ci******@lycos.com wrote:

[]
I'm not sure how to write the copy constructor to make sure it makes
the right copy of b.
How would the copy constructor know which derived type to copy?


http://www.parashift.com/c++-faq-lit....html#faq-20.8

Mar 18 '06 #4
ci******@lycos.com wrote:
This is what I have

class base{public:virtual ~base();};
class derived1 : public base {};
class derived2 : public base {};

class A
{
public:
A(base *b_):b(b_) {
}
A(const A&a)
{
//What to do here *************
}
private:
base *b;
};

I'm not sure how to write the copy constructor to make sure it makes
the right copy of b.
How would the copy constructor know which derived type to copy?


You can use the following smart pointer:
http://axter.com/smartptr

The smart pointer in the above link has a default policy to clone
(deep-copy) your derived type.
Moreover, you don't need to create a clone function for your base
class, because this smart pointer can automatically determine the
derived type by using the type pass to the constructor.
You would have to change your constructor to the following:
class A
{
public:
A(smart_ptr<base> b_):b(b_) {
}
A(const A&a):b(a.b)
{
//Now you don't have to do anything here
}
private:
smart_ptr<base> b;
};

If your A class only has the b member, you don't even need a copy
constructor for your A class, because the smart pointer will clone
automatically.

The smart_ptr is also more efficient and more flexible than the
boost::shared_ptr, which doesn't clone.

---------------------------------------------------------------------------*-------------

David Maisonave
http://axter.com
Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
---------------------------------------------------------------------------*-------------

Mar 18 '06 #5
Maxim Yegorushkin wrote:
ci******@lycos.com wrote:

[]
I'm not sure how to write the copy constructor to make sure it makes
the right copy of b.
How would the copy constructor know which derived type to copy?


http://www.parashift.com/c++-faq-lit....html#faq-20.8


I recommend against using this method posted in the FAQ.
With this method it's harder to detect slicing, which can occur if a
derived derived type fails to implement the clone function.

With the clone function method, the best you can do is to try to detect
slicing when and if cloning occurs. If cloning never occurs during
testing, the bug doesn't get detected until your customer reports it to
you.

With the default method used in the following smart_ptr, slicing can be
detected on the smart pointer's constructor:
http://axter.com/smartptr

This makes it much more likely to be caught during testing, compare to
the clone function method.
More over, the default method used in the smart_ptr requires less
maintenance than that of the clone function method.

For more information, read the smart_ptr link.

Mar 18 '06 #6
Would this smart pointer be able to store STL exception objects somehow
that are caught with a reference to the base?

Fraser.
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Mar 18 '06 #7
Fraser Ross wrote:
Would this smart pointer be able to store STL exception objects somehow
that are caught with a reference to the base?

Fraser.


You don't store exceptions, so I'm not sure what exactly you're
referring to.

What ever exceptions handling you have with a raw pointer of the base
type, you'll also have with the smart pointer of the base type.

---------------------------------------------------------------------------*-------------

David Maisonave
http://axter.com
Top ten member of C++ Expert Exchange:
http://www.experts-exchange.com/Cplusplus
---------------------------------------------------------------------------*-------------

Mar 18 '06 #8
Axter wrote:
Maxim Yegorushkin wrote:
ci******@lycos.com wrote:

With the default method used in the following smart_ptr, slicing can be
detected on the smart pointer's constructor:
http://axter.com/smartptr


I notice that link also has a sync_ptr class that automatically locks
the smart pointer, and the smart_ptr class also has lock logic, but it
uses a scope_lock class.

Why doesn't the smart_ptr class have the same automatic lock interface
as does the sync_ptr class?

I have a multithreading application in which I think I can use this
type of smart pointer, but the sync_ptr looks like a better choice.
But it doesn't have reference counting.

Mar 19 '06 #9
ci******@lycos.com wrote:
Axter wrote:
Maxim Yegorushkin wrote:
ci******@lycos.com wrote:

With the default method used in the following smart_ptr, slicing can be
detected on the smart pointer's constructor:
http://axter.com/smartptr


I notice that link also has a sync_ptr class that automatically locks
the smart pointer, and the smart_ptr class also has lock logic, but it
uses a scope_lock class.

Why doesn't the smart_ptr class have the same automatic lock interface
as does the sync_ptr class?

I have a multithreading application in which I think I can use this
type of smart pointer, but the sync_ptr looks like a better choice.
But it doesn't have reference counting.


I have future plans for adding all the features of the sync_ptr class
to the smart_ptr class.
It's rare to find a smart pointer using reference counting logic and
synchronization logic together. So the first stage of adding
synchronization logic to smart_ptr was just a proof of concept.
Now that I'm certain it can be done, I can go ahead and add the other
features.

Mar 19 '06 #10

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

Similar topics

42
by: Edward Diener | last post by:
Coming from the C++ world I can not understand the reason why copy constructors are not used in the .NET framework. A copy constructor creates an object from a copy of another object of the same...
11
by: Nindi73 | last post by:
A few days a ago I posted my code for a deep copy pointer which doesn't require the pointee object to have a virtual copy constructor. I need help with checking that it was exception safe and...
3
by: jacek.dziedzic | last post by:
Hello! Suppose I have a class base, with virtual methods and a virtual destructor and a bunch of classes, derived1, derived2, ... which publicly derive from base. I then have a pointer base*...
13
by: JD | last post by:
Hi, My associate has written a copy constructor for a class. Now I need to add an operator = to the class. Is there a way to do it without change her code (copy constructor) at all? Your help...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.