Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) C++11STL 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.

 

 

 

 

 

 

C++11STL Creating a std::shared_ptr

 

#include <memory>

int main()
{
  //Only works when using C++11
  std::shared_ptr<int> p;
}

 

 

 

 

 

Advice

 

 

 

 

 

 

References

 

  1. 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'
  2. 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'
  3. 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

 

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict