From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Emil Dotchevski Sent: March-18-11 2:51 PM On Fri, Mar 18, 2011 at 8:55 AM, Ted Byers <r.ted.byers@gmail.com> wrote:
The type for the container would be: std::vector<boost::shared_ptr<base_type> >
boost::shared_ptr<base_type> and boost::shared_ptr<derived_type> are two different types. You can't cast from the one to the other without writing code to do it. So, to make one vector containing (smart) pointers to instances of any or all the derived types, you would have to cast the pointers to instances the derived types to the base before you create the shared_ptr.
Put the return value from new directly into a shared_ptr -- it'll just work!
When the object expires, shared_ptr will not call the destructor through a
OK. Good. But how? I am not one to settle for 'it just works'. Rather, I like to dig into code to see precisely how it works. Though, when dealing with business admin types, whose eyes glaze over in incomprehension the moment the discussion begins to get technical, I too use the sentence "Trust me, it just works." ;-) I actually had the code for boost:shared_ptr open, as a result of Frank Hess' message, looking for where and how this capability was implemented. I don't suppose you can speed my examinatin of this code by giving me some line numbers ... ? base pointer, so the base destructor should be protected (and non-virtual) to protect from someone attempting to call delete on a base pointer. OK. You have said a number of times it is bad to have people calling delete on base pointers. The question is why? The most useful power of runtime polymorphism is based on invokving the right virtual functions through base pointers, so I don't see why it is bad to use polymorphism when it comes to destruction also. In my experience, this capability has been priceless, and I have yet to see a downside. Would you please explain the circumstances where using this polymorphism with destructors can be bad, and why you would extend the idea of deleting objects through base class pointers, exploiting public virtual destructors as bad to be a general practice. In other words, where is the harm in a practice I have used successfully for almost 20 years? Cheers Ted