Boost logo

Boost Users :

From: eric lindvall (eric_at_[hidden])
Date: 2004-10-07 16:10:00


I have created a simple template to make instantiation of shared_ptrs a
little prettier.

Here is an abridged version of the template:
                                            
    template <class T>
    class ManagedPointer : public boost::enable_shared_from_this<T>
    {
        public:
            typedef boost::shared_ptr<T> shared_ptr;

        public:
            static shared_ptr create ()
            {
                return (shared_ptr (new T ()));
            }
             
            template <class A1>
            static shared_ptr create (A1 a1)
            {
                return (shared_ptr (new T (a1)));
            }
    };

This allows for me to create a class:
                                     
    class TestClass : public ManagedPointer<TestClass>
    {
        public:
            TestClass();
    };

and create an instance by:
                          
    TestClass::shared_ptr test = TestClass::create();

I have noticed in certain cases I get very strange behaviour. Most
recently I've seen things hanging in the shared_ptr destructor.

Changing the code to:
                     
    TestClass::shared_ptr test (new TestClass());

does "solve" the problem.

Is there something subtle in how the templates and subclassing are
interacting that I am not understanding?

thanks.

e.


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