Welcome to our forums...

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed.

Forum Statistics

  • Forum Members:
  • Total Threads:
  • Total Posts: 4
There are 1 users currently browsing forums.
Articles, Tutorials, and Guides Here is a collection of tutorials. You may submit tutorials and read them as you please.

Reply
  #1  
Old 02-20-2006
MalachiX's Avatar
A "Mature" Pre-Teenager!
 
Join Date: Apr 2005
Location: Miami
Age: 23
Posts: 183
Rep Power: 6
MalachiX is on a distinguished road
Post How to use the auto_ptr

A tutorial I have on website explaining how to use the auto_ptr template in c++.

The url is Auto_ptr Tutorial

P.S. I wrote this a while back, it is a very good. I'd love feedback on it.
Reply With Quote
  #2  
Old 02-21-2006
hot_cakes's Avatar
Moderat0r!!1
 
Join Date: Aug 2005
Location: Bristol, UK
Age: 28
Posts: 2,939
Rep Power: 8
hot_cakes will become famous soon enough
Re: How to use the auto_ptr

Looks good, but std::auto_ptr<> is declared in <memory> and std::endl is only guaranteed to be declared in <ostream> i.e. <iostream> probably will declare it, but it's not guaranteed by the C++ standard.

Also, your last example uses a class that is dangerous to copy. I'd recommend either declaring but not implementing a private copy-constructor and copy-assignment operator, or implementing public versions of each that make deep copies (but be careful with polymorphic base classes!). As an extension to your article, have a look at std::tr1::shared_ptr aka boost::shared_ptr and boost::weak_ptr. They're even more useful than auto_ptr! And using a shared_ptr instead of auto_ptr in your last example will automagically make it safe to copy.

For a similar hand-coded shared pointer implementation have a look at mine: http://www.nunswithguns.net/guff/core/ptr.h and shared.h for core::ptr and core::array and their base class which provides reference-counted auto-destroy sematics for any class. An example using FILE* is given, which is fclose()d automatically when the last copy goes out of scope.

I plan on changing them to remove the exception specifications and to use the protected destructor idiom. In fact I'll write that on the back of my hand to do tonight!!

Also, if you're feeling really adventurous, check out Andrei Alexandrescu's policy-based smart-pointer in "Modern C++ design".

Edd
__________________
Visit me at: mr-edd.co.uk
Languages: Python | Lua
Compilers: MinGW | MSVC++9
Libraries: Boost | gtkmm
Reference: Dinkumware | a.c.l.l.c-c++ FAQ

Last edited by hot_cakes; 02-21-2006 at 06:16 AM.
Reply With Quote
  #3  
Old 02-22-2006
MalachiX's Avatar
A "Mature" Pre-Teenager!
 
Join Date: Apr 2005
Location: Miami
Age: 23
Posts: 183
Rep Power: 6
MalachiX is on a distinguished road
Smile Re: How to use the auto_ptr

Ahhh I see.

I've heard of the other smart pointers but I never got around to actually using one of the alternates. I made the tutorial on the auto pointer in the c++ standard simply because it is the most bare bones and standard. I heard good things about the ones in boost though. I hear they can delete arrays of memory amongst other things.

Btw, thanks for telling me about the std::endl. Thats really interesting.

:::raises eyebrows:::
"yes.....now I see...."
Reply With Quote
  #4  
Old 02-22-2006
hot_cakes's Avatar
Moderat0r!!1
 
Join Date: Aug 2005
Location: Bristol, UK
Age: 28
Posts: 2,939
Rep Power: 8
hot_cakes will become famous soon enough
Re: How to use the auto_ptr

Well boost::shared_ptr is now standard in the std::tr1 namespace if your library supports it. It will probably end up as std::shared_ptr when the next revision comes around. It can be made using standard C++ constructs anyway (as can/does the ptr.h file I mentioned). Just food for thought!

By the way, if anyone is serious about C++ and doesn't know what auto_ptr is or does, give MalachiX's article a read. auto_ptr is sorely underused given that it provides so many benefits when writing resource-managed and exception safe code.

Edd
Reply With Quote


Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On