shared_ptr vs ordinary pointers
With an ordinary C++ pointer, if I have a BaseClass and a DerivedClass derived from BaseClass, then I can do BaseClass* objectPtr; objectPtr = new DerivedClass(); Later, if BaseClass has a virtual destructor, I can call delete(objectPtr) and the DerivedClass destructor will be called. Now, does this work with shared_ptr? shared_ptr<BaseClass> objectPtr; objectPtr = shared_ptr<DerivedClass>(new DerivedClass()); and later, delete(objectPtr). Does any of the magic going on behind the scenes prevent shared_ptrs from being used like this? Merrill
Merrill Cornish wrote:
With an ordinary C++ pointer, if I have a BaseClass and a DerivedClass derived from BaseClass, then I can do
BaseClass* objectPtr; objectPtr = new DerivedClass();
Later, if BaseClass has a virtual destructor, I can call delete(objectPtr) and the DerivedClass destructor will be called.
Now, does this work with shared_ptr?
shared_ptr<BaseClass> objectPtr; objectPtr = shared_ptr<DerivedClass>(new DerivedClass());
and later, delete(objectPtr).
No, you shouldn't call delete directly. It's done for you automatically when the last shared_ptr that points to your object is destroyed.
Does any of the magic going on behind the scenes prevent shared_ptrs from being used like this?
shared_ptr would be close to useless if it didn't. In fact, shared pointer does *more* to remember the dynamic type than ordinary pointers. Forex, you can do this: { shared_ptr<void> voidPtr; voidPtr = shared_ptr
Merrill Cornish wrote:
With an ordinary C++ pointer, if I have a BaseClass and a DerivedClass derived from BaseClass, then I can do
BaseClass* objectPtr; objectPtr = new DerivedClass();
Later, if BaseClass has a virtual destructor, I can call delete(objectPtr) and the DerivedClass destructor will be called.
Now, does this work with shared_ptr?
shared_ptr<BaseClass> objectPtr; objectPtr = shared_ptr<DerivedClass>(new DerivedClass());
Yes.
and later, delete(objectPtr).
No, but it's because of C++ rules not shared_ptr rules ;-) When the objectPtr goes out of scope the pointee is deleted.
Does any of the magic going on behind the scenes prevent shared_ptrs from being used like this?
It's designed to work like a regular ptr. Make sure to read the docs: http://www.boost.org/libs/smart_ptr/shared_ptr.htm Pay special attention to the TR1 functions http://www.boost.org/libs/smart_ptr/shared_ptr.htm#functions for pointer conversions: static_pointer_cast, const_pointer_cast, and dynamic_pointer_cast. -- -- Grafik - Don't Assume Anything -- Redshift Software, Inc. - http://redshift-software.com -- rrivera/acm.org - grafik/redshift-software.com -- 102708583/icq - grafikrobot/aim - Grafik/jabber.org
Merrill Cornish wrote:
With an ordinary C++ pointer, if I have a BaseClass and a DerivedClass derived from BaseClass, then I can do
BaseClass* objectPtr; objectPtr = new DerivedClass();
Later, if BaseClass has a virtual destructor, I can call delete(objectPtr) and the DerivedClass destructor will be called.
Now, does this work with shared_ptr?
shared_ptr<BaseClass> objectPtr; objectPtr = shared_ptr<DerivedClass>(new DerivedClass());
Yes, it does. It will work even without a virtual destructor.
and later, delete(objectPtr).
This doesn't work.
Does any of the magic going on behind the scenes prevent shared_ptrs from being used like this?
Of course not. :-)
participants (4)
-
Jonathan Biggar
-
Merrill Cornish
-
Peter Dimov
-
Rene Rivera