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.
Understanding the concept of Smart Pointers
Started by thedevsykes, Nov 04 2012 11:40 AM
3 replies to this topic
#1 Members - Reputation: 336
Posted 04 November 2012 - 11:40 AM
'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)
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)
Ad:
#2 Crossbones+ - Reputation: 2418
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.
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 Marketplace Seller - Reputation: 8937
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.
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.
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
#4 Members - Reputation: 336
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.
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)
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)






