shared_ptr inheritance query

I wonder if anyone could help me with a particular problem I'm having with shared_ptr. I'm working on a policy template, and I want the policy template to both privately inherit, and inject enable_shared_from_this - but the 2 things seem to be mutually exclusive. Let me give you an idea of what I'm trying to do (with some gross simplification to make the code a little simpler): template <class T> class policy : public enable_shared_from_this<T> { }; template <class T> class base : private policy<T> { public: shared_ptr<T> get_ptr() { return shared_from_this(); } }; class MyClass : public base<MyClass> { }; test() { shared_ptr<MyClass> obj( new MyClass ); shared_ptr<MyClass> obj1 = obj->get_ptr(); } If you run this you get a "conversion from MyClass* to boost::enable_shared_from_this<T> * exists, but is inaccessible" error (on VC++7.1). The same is true with protected inheritance. Is there any way around this? Thanks Dave Handley

On Mon, Jan 31, 2005 at 12:06:46AM -0000, Dave Handley wrote:
I wonder if anyone could help me with a particular problem I'm having with shared_ptr. I'm working on a policy template, and I want the policy template to both privately inherit, and inject enable_shared_from_this - but the 2 things seem to be mutually exclusive.
http://boost.org/libs/smart_ptr/enable_shared_from_this.html says: Requires: enable_shared_from_this<T> must be an accessible base class of T. In order for the shared_ptr<T> constructor to initialise the enable_shared_from_this<T>::_internal_weak_this member of the object it owns, it must be able to access the base class. Obviously using public inheritance works, but you might also be able to make it work by making shared_ptr<T> a friend of policy<T> so it can do the conversion. You'll also need to disable the BOOST_ASSERTs in enable_shared_from_this.hpp by defining BOOST_DISABLE_ASSERTS or by copying enable_shared_from_this.hpp and re-implementing the class without the assertions. I have no idea if this is supposed to work, nor if it will still work if the internals of shared_ptr or enable_shared_from_this change. Caveat emptor. jon -- "It has always been the prerogative of children and half-wits to point out that the emperor has no clothes. But the half-wit remains a half-wit, and the emperor remains an emperor." - Neil Gaiman
participants (2)
-
Dave Handley
-
Jonathan Wakely