Jump to content
  • Advertisement
Sign in to follow this  
garyfletcher

Effective C++ and std::tr1::shared_ptr

This topic is 4870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all. Just a quick question. I'm currently reading the fine Effective C++. I'm on Chapter 3 - Resource management and Mr Meyers is talking about using atuo_pte and std::tr1::shared_ptr<>. Now isn't std::tr1::sharded_ptr<> just an implementation of boost::sharded_ptr<>, and if so instead of using auot_ptr should I look at using boost::scoped_ptr?

Share this post


Link to post
Share on other sites
Advertisement
You can use both,

Autoptr lets you have only one owner for the pointer it wraps so if you assign an autoptr to another pointer the autoptr will be set to null.

Sharedptr uses a reference count to determine its life time. Each time you assign it to another ptr the ref count goes up. When each ptr is deleted the ref count goes down, at zero its the end of the game.

I find autoptr to be good documentation for times where you don't want others to copy a pointer you created.

Cheers
Chris

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher

Now isn't std::tr1::sharded_ptr<> just an implementation of boost::sharded_ptr<>, and if so instead of using auot_ptr should I look at using boost::scoped_ptr?


Yes, you should look at using boost::shared_ptr.

However, std::tr1::shared_ptr is only an interface specification, not a implementation specification. A compiler vendor can implement std::tr1::shared_ptr in such a way that it would not behave identically to boost::shared_ptr.

Share this post


Link to post
Share on other sites
Quote:
Original post by garyfletcher
I see.

So what about auto_ptr and boost::scoped_ptr?


The main difference there AFAIK is that scoped_ptr does not implement passing of ownership semantics, and is designed to be noncopyable in the interests of not shooting yourself in the foot. Basically, the way I see it:

scoped_ptr - the basic cornerstone of RAII, does nothing more than make sure you delete your pointers when you're done. Use this if you need basic RAII (e.g.: boost::scoped_ptr< foo > foo_ptr( new foo( ... ) ); )

shared_ptr - as per scoped_ptr, only ownership can be shared and/or a custom "deleter" function/functor supplied - use this if you need shared ownership, a container-compatibile smart pointer, or custom cleanup (e.g.: boost::shared_ptr< SDL_Surface > surface( SDL_CreateRGBSurface( ... ) , &SDL_FreeSurface ); )

auto_ptr - as per scoped_ptr, only ownership can be transfered. Use this if you need to transfer ownership from one object to another (can't think of a trivial example one-liner ^_^;;)

EDIT: Revised opinion to clarify

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!