
hello given the following class hierarchy class Foo { protected: virtual ~Foo() {} }; class Bar : public Foo {}; and declaration boost::shared_ptr<Foo> foo; is there any difference between the two assignment statements below? foo = boost::shared_ptr<Foo>(new Bar); foo = boost::shared_ptr<Bar>(new Bar); in other words, when you assign a value to a shared_ptr, does it make any difference what class you specify for that value, as long as the class of the underlying object can be dynamically cast to the class that was used to declare the shared_ptr? kind regards, eric

On 7/12/06, eric
in other words, when you assign a value to a shared_ptr, does it make any difference what class you specify for that value, as long as the class of the underlying object can be dynamically cast to the class that was used to declare the shared_ptr?
In the example you gave, there's no difference in effect. 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.) ~ Scott McMurray

eric wrote:
hello
given the following class hierarchy
class Foo { protected: virtual ~Foo() {} };
class Bar : public Foo {};
and declaration
boost::shared_ptr<Foo> foo;
is there any difference between the two assignment statements below?
foo = boost::shared_ptr<Foo>(new Bar); foo = boost::shared_ptr<Bar>(new Bar);
No, the first form is a shorthand for the second form, and so is foo.reset( new Bar );
participants (3)
-
eric
-
me22
-
Peter Dimov