Boost logo

Boost Users :

From: David Klein (d.klein_at_[hidden])
Date: 2006-03-20 08:28:56


boost-users-bounces_at_[hidden] wrote:
>> -----Original Message-----
>> From: David Klein [mailto:d.klein_at_[hidden]]
>> Sent: 20 March 2006 10:27
>> To: boost-users_at_[hidden]
>> 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_at_[hidden]
> 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


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