|
Boost : |
From: Daniel Frey (d.frey_at_[hidden])
Date: 2008-04-16 15:46:35
I had yet another idea which might improve the situation for the virtual
inheritance problem that my optimized implementation of
enable_shared_from_this<T> has. Additionally, the solution provides
another useful feature.
Currently, shared_from_this() has no parameter and thus needs to either
store the pointer to T* (as part of the internal weak_ptr<T>) like the
1.35.0-solution does, or it needs to use the this-pointer. The
this-pointer for the class is of type enable_shared_from_this<T>*, not
of type T* - this is why my improved version needs to static-cast the
this-pointer, which in turn requires a non-virtual inheritance between
enable_shared_from_this<T> and T.
The problem can be solved by either extending or modifying the
interface: When we add the following method(s):
template< typename U >
shared_ptr<U> shared_from_this( U* u );
template< typename U >
shared_ptr<U const> shared_from_this( U const* u );
it's then possible for a method of T to call shared_from_this(this)
instead of shared_from_this() to gain a shared_ptr<T>.
Additionally, the interface is generally useful, as it could be called
with other pointers, e.g. to sub-objects (member variables) of T. It's
IMHO a nice match to the shared_ptr-constructor which takes a shared_ptr
and an additional plain pointer.
Peter, Frank, what do you think?
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk