C
corophoria
Some people I interact with, especially those with a c background,
have an aversion to scoped_ptrs because they see a new(), but they
don't see a corresponding delete(), and they get very scared
e.g. usage
Class Bar
{
int m_data;
};
void Foo()
{
scoped_ptr<Bar> bar( new Bar() );
process( bar );
}
In the default constructor case, we can make a wrapper around
scoped_ptr like:
template <typename T>
class ScopedWrapper
{
public:
ScopedWrapper<T>
: m_ptr( new T() )
{
}
private:
scoped_ptr<T> m_ptr;
};
then, for the previous function, we can equivalenty write:
void Foo()
{
ScopedWrapper<Bar> bar;
process( bar );
}
Now, people don't have to see the new call, and it's a bit easier for
them to read,
Now, let's say Bar has some arbitrary constructor with signature like:
Bar::Bar(int, double, std::string );
now, to instantiate a Bar using a scoped_ptr i would traditionally do
scoped_ptr<Bar> bar( new Bar(3, 2, "fool") );
Now, what I would like to be able to do is write something like
ScopedWrapper<Bar> bar(3,2,"fool"); or something like that...
I am not sure that really explained what I want. I would like to put
arbitrary objects with arbitrary constructors on the heap rather than
on the stack, without any new/delete's but have some kind of auto
ownership mechanisms?
Sorry if this question has been answered before, or if the C++ syntax
is wrong.
Thanks,
corophoria
have an aversion to scoped_ptrs because they see a new(), but they
don't see a corresponding delete(), and they get very scared
e.g. usage
Class Bar
{
int m_data;
};
void Foo()
{
scoped_ptr<Bar> bar( new Bar() );
process( bar );
}
In the default constructor case, we can make a wrapper around
scoped_ptr like:
template <typename T>
class ScopedWrapper
{
public:
ScopedWrapper<T>
: m_ptr( new T() )
{
}
private:
scoped_ptr<T> m_ptr;
};
then, for the previous function, we can equivalenty write:
void Foo()
{
ScopedWrapper<Bar> bar;
process( bar );
}
Now, people don't have to see the new call, and it's a bit easier for
them to read,
Now, let's say Bar has some arbitrary constructor with signature like:
Bar::Bar(int, double, std::string );
now, to instantiate a Bar using a scoped_ptr i would traditionally do
scoped_ptr<Bar> bar( new Bar(3, 2, "fool") );
Now, what I would like to be able to do is write something like
ScopedWrapper<Bar> bar(3,2,"fool"); or something like that...
I am not sure that really explained what I want. I would like to put
arbitrary objects with arbitrary constructors on the heap rather than
on the stack, without any new/delete's but have some kind of auto
ownership mechanisms?
Sorry if this question has been answered before, or if the C++ syntax
is wrong.
Thanks,
corophoria