Re: [Boost-users] enable_shared_from_this - or an alternative..

boost-users-bounces@lists.boost.org wrote:
-----Original Message----- From: David Klein [mailto:d.klein@ascaron.com] Sent: 20 March 2006 10:27 To: boost-users@lists.boost.org Subject: Re: [Boost-users] enable_shared_from_this - or an alternative..
[snip]
Thanks Dave, that does cover the problem well.
However, I cannot use the null deleter mechanism suggested as the shared_ptr is passed on to monitor and is used there. The second scheme, using a factory fucntion, would seem to be approriate, however, how to I handle creation when the CSource1 class is actually to be used as a base class? If the factory function is in CSource1, it doesnt know about any derived classes. I don't want to have to write factory functions in every dervied class - that seems a waste. Is there a correct technique for getting around this issue? I am assuming a template of some description, but now sure what that entails.
James
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
is the following snipped what you have in mind? struct base {}; struct derived1 : public base {}; struct derived2 : public derived1 {}; typedef boost::shared_ptr<base> base_ptr; struct monitor { void add( const base_ptr & _ptr) { // ... } }; struct factory { factory( monitor & _monitor ) : monitor_(_monitor) { } template<typename T> boost::shared_ptr<T> create( void ) { boost::shared_ptr<T> tmp(new T); monitor_.add(tmp); return tmp; } private: monitor & monitor_; }; void foo( void ) { monitor m; factory f(m); f.create<derived1>(); f.create<derived2>(); } -- dave
participants (1)
-
David Klein