Re: [Boost-users] shared_ptr implicit dynamic cast
foo = boost::shared_ptr<Foo>(new Bar); foo = boost::shared_ptr<Bar>(new Bar); Scott McMurray wrote:
The important difference is in the default deleter. shared_ptr<Foo>'s default deleter will call delete on a Foo* while shared_ptr<Bar>'s default deleter will call it on a Bar*. Since your base class's destructor is virtual, it doesn't make any difference in the example. If your destructor were non-virtual, then you'd have to use shared_ptr<Bar>(new Bar) to get the correct destructors called. (But without any virtual functions, the dynamic casting wouldn't compile.)
Are you sure about that? A check of the documentation lists the signature for that constructor as template<class Y> explicit shared_ptr(Y * p); The explanation is given as follows: [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. The reset function has a similar signature.
On 7/12/06, Andrew Holden
Are you sure about that? A check of the documentation lists the signature for that constructor as
template<class Y> explicit shared_ptr(Y * p);
My apologies, I didn't know that. Certainly a very nice feature as it means that even without the virtual destructor both would be safe. Promises to double-check the documentation before replying next time, ~ Scott McMurray
participants (2)
-
Andrew Holden
-
me22