How to copy derived class from a base pointer

C

cindypwl

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?
 
X

XHengDF

in the class base{public:virtual ~base();};
you should add virtual clone() = 0;
the things will be OK!
cheer!
 
A

Axter

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
---------------------------------------------------------------------------­-------------
 
A

Axter

Maxim said:
(e-mail address 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-lite/virtual-functions.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.
 
F

Fraser Ross

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 ***
 
A

Axter

Fraser said:
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
---------------------------------------------------------------------------­-------------
 
C

cindypwl

Axter said:
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.
 
A

Axter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top