|
Boost Users : |
Subject: Re: [Boost-users] [Interprocess] shared_ptr in shared memory and inheritance
From: Ion Gaztañaga (igaztanaga_at_[hidden])
Date: 2009-02-10 12:08:45
Gaetan Gaumer wrote:
> In boost::shared_ptr doc
> http://www.boost.org/doc/libs/1_38_0/libs/smart_ptr/shared_ptr.htm#Members
> It's said :
> "This constructor has been changed to a template in order to remember
> the actual pointer type passed. The destructor will call *delete* with
> the same pointer, complete with its original type, even when *T* does
> not have a virtual destructor, or is *void*."
Sorry, I spoke too fast. I was trying to say that if you cast your type
to the base class and they construct a shared_ptr<Base>, then you loose
all your polymorphism. shared_ptr's template constructor does this for
you: shared_ptr ptr only knows the real type of the object in that
constructor and that compile-time information is going to disappear when
the templated constructor ends, so shared_ptr constructs a polymorphic
shared_count that will call the correct deleter when needed:
template<class Y> explicit shared_count( Y * p ): pi_( 0 )
{
//sp_counted_impl_p has VIRTUAL functions
pi_ = new sp_counted_impl_p<Y>( p );
//...
}
sp_counted_impl_p has type Y so it calls Y's constructor and
sp_counted_impl_p is polymorphic so it can be casted to sp_counted_base
without loosing functionality. So you always need virtual functions to
get type erasure with shared_ptr. And that's why you can't do this with
interprocess's shared_ptr.
> Is it really impossible to achieve the same behavior in
> boost::interprocess::shared_ptr than in boost::shared_ptr ?
I afraid you can't. shared memory forbids any type of run-time
polymorphism and that includes shared_ptr.
> PS : By the way, thanks Ion and others for this great work on boost in
> general and the Interprocess library in particular.
Thanks for using Boost in general and Interprocess in particular ;-)
Regards,
Ion
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net