473,322 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

smart pointer patterns

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<Barbar( 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<Tm_ptr;
};

then, for the previous function, we can equivalenty write:

void Foo()
{
ScopedWrapper<Barbar;
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<Barbar( new Bar(3, 2, "fool") );

Now, what I would like to be able to do is write something like

ScopedWrapper<Barbar(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

Jul 11 '07 #1
3 1544
co********@gmail.com wrote:
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<Barbar( 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<Tm_ptr;
};

then, for the previous function, we can equivalenty write:

void Foo()
{
ScopedWrapper<Barbar;
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<Barbar( new Bar(3, 2, "fool") );

Now, what I would like to be able to do is write something like

ScopedWrapper<Barbar(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.
What's wrong with specializing ScopedWrapper?

template<>
class ScopedWrapper<Bar{
ScopedWrapper() : m_ptr(new Bar()) { }
ScopedWrapper(int x, int y, const std::string& z)
: m_ptr(new Bar(x, y, z) { }
};
Alternatively, you might try to use (multiple) templated constructors if
you don't want to do too many specializations.

Jul 11 '07 #2
What's wrong with specializing ScopedWrapper?
>
template<>
class ScopedWrapper<Bar{
ScopedWrapper() : m_ptr(new Bar()) { }
ScopedWrapper(int x, int y, const std::string& z)
: m_ptr(new Bar(x, y, z) { }

};

Alternatively, you might try to use (multiple) templated constructors if
you don't want to do too many specializations.
If somebody introduces a new class to the codebase, then I would like
them to be able to use ScopedWrapper without writing any extra lines
of code.

Thanks,
corophoria

Jul 11 '07 #3
On Jul 12, 12:00 am, coropho...@gmail.com wrote:
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
How often do you want a new and a delete in the same scope
anyway? In my experience, the case is rare.
e.g. usage
Class Bar
{
int m_data;
};
void Foo()
{
scoped_ptr<Barbar( new Bar() );
process( bar );
}
The usual way to write Foo in C++ would be:

void
Foo()
{
Bar bar ;
process( bar ) ;
}

About the only time you'll use new for a local variable is when
polymorphism is involved, and the real type actually varies at
runtime. E.g.:

void
Foo()
{
Base * p = someCondition
? static_cast< Base* >( new Derived1 )
: static_cast< Base* >( new Derived2 ) ;
// ...
}

In such cases, I would definitely use a scoped_ptr if I needed
to call the destructor, or if I wasn't using the Boehm collector
for some reason. Not doing so is just looking for trouble.
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<Tm_ptr;

};
then, for the previous function, we can equivalenty write:
void Foo()
{
ScopedWrapper<Barbar;
process( bar );}
Now, people don't have to see the new call, and it's a bit easier for
them to read,
You mean more confusing, since they don't see that the object is
allocated dynamically. And again, the only reason to allocate
dynamically here is if the dynamic type varies. Something your
wrapper won't handle.
Now, let's say Bar has some arbitrary constructor with signature like:
Bar::Bar(int, double, std::string );
That's easy. Just a set of template constructors for your
ScopedWrapper. Try handling my example above, however. Or
something like:

scoped_ptr< Base p = factoryMap[ someArg ]->create() ;

(where factoryMap is an std::map< ArgType, AbstractFactory
const* >). Cases where you really would use a pointer for an
object whose lifetime corresponds to a scope.

IMHO, you have two problems which need solving:

-- your use of dynamic allocation is not appropriate, and
-- your collegues need to be educated---it's perfectly normal
to see a new and the delete to be elsewhere. (I'd guess
that for well over half of the objects I allocate
dynamically, the delete is in a member function.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 12 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Susan Baker | last post by:
Hi, I'm just reading about smart pointers.. I have some existing C code that I would like to provide wrapper classes for. Specifically, I would like to provide wrappers for two stucts defined...
8
by: Axter | last post by:
I normally use a program call Doxygen to document my source code.(http://www.stack.nl/~dimitri/doxygen) This method works great for small and medium size projects, and you can get good...
59
by: MotoK | last post by:
Hi Experts, I've just joined this group and want to know something: Is there something similar to smart pointers in C or something to prevent memory leakages in C programs. Regards MotoK
92
by: Jim Langston | last post by:
Someone made the statement in a newsgroup that most C++ programmers use smart pointers. His actual phrase was "most of us" but I really don't think that most C++ programmers use smart pointers,...
14
by: Ian | last post by:
I am looking at porting code from a C++ application to C#. This requires implementing data sharing functionality similar to what is provided by a smart pointer in C++. I have only recently begun...
33
by: Ney André de Mello Zunino | last post by:
Hello. I have written a simple reference-counting smart pointer class template called RefCountPtr<T>. It works in conjunction with another class, ReferenceCountable, which is responsible for the...
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
4
by: Deep | last post by:
I'm in doubt about what is smart pointer. so, please give me simple description about smart pointer and an example of that. I'm just novice in c++. regards, John.
54
by: Boris | last post by:
I had a 3 hours meeting today with some fellow programmers that are partly not convinced about using smart pointers in C++. Their main concern is a possible performance impact. I've been explaining...
50
by: Juha Nieminen | last post by:
I asked a long time ago in this group how to make a smart pointer which works with incomplete types. I got this answer (only relevant parts included): ...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.