Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2006-10-19 13:34:46


Nat Goodspeed wrote:

> [Nat] <silly question>
> Could the ref count be put into a virtual base class so that only one
> instance would appear in a given leaf class, regardless of how many
> enable_shared_from_this<> bases it has?

Not a silly question at all.

Every enable_shared_from_this base contains a weak_ptr, not a reference
count. The shared_ptr constructor looks up the enable_shared_from_this base
and initializes its weak_ptr accordingly. This doesn't work when there are
two or more enable_shared_from_this bases, though.

I could put the weak_ptr in a virtual polymorphic base. This would force
polymorphism on all clients of enable_shared_from_this... probably
acceptable. It will also force a dynamic_pointer_cast in every
shared_from_this, and this may be harder to swallow, particularly in cases
where RTTI is off. So I'm not sure.

If you do want the above behavior, it's easy to duplicate, as I already
responded in my first post on the topic. Just make FooB return
dynamic_pointer_cast<B>( FooA() ) and remove the enable_shared_from_this<B>
base (A needs to be made polymorphic, of course).


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