Jump to content

  • Log In with Google      Sign In   
  • Create Account

Understanding the concept of Smart Pointers


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 thedevsykes   Members   -  Reputation: 336

Like
0Likes
Like

Posted 04 November 2012 - 11:40 AM

The title, likely doesn't do much justice to what my question is, and i'll try to be as a descriptive as I possibly can be.

My issue, is not more on misunderstanding Smart Pointers, but rather, a small glitch in trying to get my head around them. I know smart pointers are used for dynamically creating objects, and that they manage the destruction of these objects, and keep a reference to them using reference counting. But, what I struggle to understand is when I should use these, should I, for example use the std::tr1::shared_ponter<>, for everything.. or just large game sub-systems that need to be referenced by other systems in my game? Such as my graphics rendering system that will need to be used by other parts of my game.. or scene management, etc.. (These are just examples).

Also, where do weak pointers fit into this, when should a weak pointer be used instead of a shared pointer?

I hope my question makes sense, i'll try to clarify anything that doesn't.

Regards
and thanks.
'Knowledge isn't key, but understanding...'

My qualifcations are not here to showcase, but for those I answer and ask, to get a better idea on my knowledge.

BCS Level 2 Certificate for IT Users (ECDL Part 2)
OCR Level 2 National Award in Business
Level 2 First Diploma in Media
Level 3 Diploma in Games Design and Development Extended
BSc Hons in Computer Games Programming (Current - 1st Year)

Sponsor:

#2 joew   Crossbones+   -  Reputation: 3662

Like
9Likes
Like

Posted 04 November 2012 - 12:25 PM

The main point of the pointer classes is to control ownership.

If the object is the sole owner of an instance you should use std::unique_ptr<>
If the instance can be owned by many objects you should use std::shared_ptr<>
If the instance is used by another class but is not owned you should use std::weak_ptr<> (the pointer is not owned and may not be valid)

Regarding when to use them it really depends on your overall architecture. For most things I return references and use shared_ptr<> sparingly as I like to have ownership clearly defined, and for things like handles I keep a weak_ptr<> to the subsystem.

#3 Servant of the Lord   Crossbones+   -  Reputation: 19516

Like
11Likes
Like

Posted 04 November 2012 - 12:55 PM

Like Joew said, they are mostly about ownership management. They also declare their intent better.

unique_ptr() - I am the sole owner and accessor of the memory.
shared_ptr() - I am one of the possible owners and one of the possible accessors of the memory.
weak_ptr() - I am one of the possible accessors of the memory, but I don't own the memory.
raw pointer * - I access the memory, but don't own it, and (ideally) the memory's lifetime is greater than my lifetime as a pointer.
not-a-pointer - I solely own the memory, and I access the memory, but references and pointers can also access it.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#4 thedevsykes   Members   -  Reputation: 336

Like
0Likes
Like

Posted 04 November 2012 - 05:13 PM

That makes sense, thank you, to you both. I'll note this down, so I don't forget it.

Cheers.
'Knowledge isn't key, but understanding...'

My qualifcations are not here to showcase, but for those I answer and ask, to get a better idea on my knowledge.

BCS Level 2 Certificate for IT Users (ECDL Part 2)
OCR Level 2 National Award in Business
Level 2 First Diploma in Media
Level 3 Diploma in Games Design and Development Extended
BSc Hons in Computer Games Programming (Current - 1st Year)




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS