PaulWendt
April 16th, 2003, 10:12 AM
I have a series of classes whose creation interface I'd like to be
a static create() function which returns a smart pointer to an
object of the class. I don't want the users creating objects by
themselves so I have made the constructors protected.
Here's a brief example:
class TestClass
{
public:
typedef boost::shared_ptr<TestClass> Ptr;
static Ptr create();
private: // protected if set up for inheritance
TestClass()
// etc
Ptr someDataThatIsOfTypePtr;
};
I may want to add a clone() function or a virtual destructor, but
that is all something that can be discussed later. The problem is
that I have to repeat all of this code for all classes that I want
this interface for. So, I came up with a "wrapper" template for
this sort of thing:
template <typename T>
class SharedPtr
{
public:
typedef boost:shared_ptr<T> Ptr;
static Ptr create() { return Ptr(new T); }
};
If I want TestClass to benefit from this, TestClass has to derive
from SharedPtr<TestClass> because TestClass internally needs
the Ptr typedef [I don't want to have to repeat all of that]. After
I inherit, SharedPtr doesn't even have access to the constructor
[because it's protected/private] so I wind up having to make
SharedPtr a friend. This doesn't seem right; there has to be a
better way.
So ... what the heck should I be doing in order to make an object
creatable only with a smart pointer interface? I'm sure that I've
seen something like this in the past on this site, but a search
proved to be fruitless.
If anybody needs any more details, I will be happy to provide
them.
--Paul
a static create() function which returns a smart pointer to an
object of the class. I don't want the users creating objects by
themselves so I have made the constructors protected.
Here's a brief example:
class TestClass
{
public:
typedef boost::shared_ptr<TestClass> Ptr;
static Ptr create();
private: // protected if set up for inheritance
TestClass()
// etc
Ptr someDataThatIsOfTypePtr;
};
I may want to add a clone() function or a virtual destructor, but
that is all something that can be discussed later. The problem is
that I have to repeat all of this code for all classes that I want
this interface for. So, I came up with a "wrapper" template for
this sort of thing:
template <typename T>
class SharedPtr
{
public:
typedef boost:shared_ptr<T> Ptr;
static Ptr create() { return Ptr(new T); }
};
If I want TestClass to benefit from this, TestClass has to derive
from SharedPtr<TestClass> because TestClass internally needs
the Ptr typedef [I don't want to have to repeat all of that]. After
I inherit, SharedPtr doesn't even have access to the constructor
[because it's protected/private] so I wind up having to make
SharedPtr a friend. This doesn't seem right; there has to be a
better way.
So ... what the heck should I be doing in order to make an object
creatable only with a smart pointer interface? I'm sure that I've
seen something like this in the past on this site, but a search
proved to be fruitless.
If anybody needs any more details, I will be happy to provide
them.
--Paul