How to use std::shared_ptr

C++11 has full support for a smart pointer called std::shared_ptr. Smart pointers keep track of how many references are pointing to a given object. When that reference count goes to zero, the object is destroyed automatically by calling its destructor. std::shared_ptr is a smart pointer that allows multiple references to be created to the same object.

To quickly understand how to use this smart pointer I created this sample C++ code:


  • ยง12.1: C++ Primer (5th Edition) by Lippman et al.
  • ยง5.2.1, ยง34.3.2: The C++ Programming Language (4th Edition) by Stroustrup

Tried with: GCC 4.9.1 and Ubuntu 14.04


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.