Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > How to copy derived class from a base pointer

Reply
Thread Tools

How to copy derived class from a base pointer

 
 
cindypwl@lycos.com
Guest
Posts: n/a
 
      03-18-2006
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?

 
Reply With Quote
 
 
 
 
XHengDF@gmail.com
Guest
Posts: n/a
 
      03-18-2006
in the class base{public:virtual ~base();};
you should add virtual clone() = 0;
the things will be OK!
cheer!

 
Reply With Quote
 
 
 
 
chenchen
Guest
Posts: n/a
 
      03-18-2006
add a member function - baseclass copy constructor

 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      03-18-2006

http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-18-2006
(E-Mail Removed) 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
---------------------------------------------------------------------------*-------------

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-18-2006
Maxim Yegorushkin wrote:
> (E-Mail Removed) 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.

 
Reply With Quote
 
Fraser Ross
Guest
Posts: n/a
 
      03-18-2006
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 ***
 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-18-2006
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
---------------------------------------------------------------------------*-------------

 
Reply With Quote
 
cindypwl@lycos.com
Guest
Posts: n/a
 
      03-19-2006
Axter wrote:
> Maxim Yegorushkin wrote:
> > (E-Mail Removed) 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.

 
Reply With Quote
 
Axter
Guest
Posts: n/a
 
      03-19-2006
(E-Mail Removed) wrote:
> Axter wrote:
> > Maxim Yegorushkin wrote:
> > > (E-Mail Removed) 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
converting derived class pointer to private base class pointer subramanian100in@yahoo.com, India C++ 8 08-18-2010 10:54 AM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 01:44 PM
Derived::Derived(const Base&) and Derived& operator=(const Base&) developereo@hotmail.com C++ 1 05-23-2007 12:07 AM
[RTTI] cast base class pointer to <templated> derived class pointer tirath C++ 3 10-12-2003 01:44 PM



Advertisments