473,756 Members | 6,661 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to copy derived class from a base pointer

This is what I have

class base{public:vir tual ~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 2356
in the class base{public:vir tual ~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:vir tual ~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<bas e> 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_p tr, 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
5804
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 kind. It sounds simple but evidently .NET has difficulty with this concept for some reason. I do understand that .NET objects are created on the GC heap but that doesn't mean that they couldn't be copied from another object of the same kind when...
11
3436
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 exception neutral/ I got a reply from Bux with his code for a smart pointer with far fewer lines of code and more cleaner design, not over engineered like mine. ...
3
3911
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* foo; which a complicated code allocates as one of derived's and sets up.
13
3970
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 is much appreciated. JD
0
9456
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9873
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9846
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5142
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3359
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2666
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.