(C++) std::shared\_ptr
February 24, 2017 · View on GitHub
(C++) 
std::shared_ptr
std::shared_ptr is a type of smart pointer that can be copied safely and cheap, without copying the object pointed to. When the last std::shared_ptr using an object goes out of scope, it will delete the object pointedto.

Creating a std::shared_ptr
#include <memory> int main() { //Only works when using C++11 std::shared_ptr<int> p; }
Advice
- Prefer pass-by-reference-to-const to pass-by-value for std::shared_ptrs [1]
- Use to refer to sharedobjects [2]
- Prefer std::unique_ptr over std::shared_ptr [3]
References
- Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. 'Prefer Pass-by-Reference-to-const to Pass-by-Value for std::shared_ptrs'
- Bjarne Stroustrup. A tour of C++. 2014. ISBN: 978-0-321-958310. Chapter 11.7.6, page 131: 'Use shared_ptr to refer to shared objects'
- Bjarne Stroustrup. A tour of C++. 2014. ISBN: 978-0-321-958310. Chapter 11.7.8, page 131: 'Prefer unique_ptr over shared_ptr'
External links