Shared_Ptr

Win32++

Shared_Ptr Class

Description

A Shared_Ptr is a smart pointer that can be used in a STL container, such as vector. It mimics the behaviour of "share_ptr" which will be included in the next standard of C++.

Shared_Ptr Members

Shared_Ptr Constructor for the Shared_Ptr.
get Returns the stored pointer.
swap Exchanges the contents of two smart pointers.
unique Returns true if the use_count equals 1. use_count Returns the number of Shared_Ptr objects. operator = Assigns a pointer to the Shared_Ptr. operator -> Returns the stored pointer. operator * Returns the dereferenced stored pointer. operator == Compares the dereferenced stored pointers. Returns true if lhs == rhs. operator != Compares the dereferenced stored pointers. Returns true if lhs != rhs. operator < Compares the dereferenced stored pointers. Returns true if lhs < rhs. operator > Compares the dereferenced stored pointers. Returns true if lhs > rhs.

Remarks

Shared_Ptr wraps a reference-counted smart pointer around a dynamically allocated object. Unlike auto_ptr, the Shared_Ptr can be used as a smart pointer for objects stored in containers like std::vector. Do not use Shared_Ptr (or shared_ptr or auto_ptr) for dynamically allocated arrays. See below for advice on how to wrap dynamically allocated arrays in a vector.

The next standard of C++ will also contain a shared_ptr. Some modern compilers already have a shared_ptr available as std::tr1::shared_ptr. If your compiler already provides a shared_ptr, or if you have Boost, you should use that smart pointer instead. This class has been provided for those users who don't have easy access to an "official" shared_ptr. Note that this class is "Shared_Ptr", a slightly different name to the future "shared_ptr" to avoid naming conflicts.

Advantages of Shared_Ptr (or shared_ptr where available):

  • Shared_Ptr can be safely copied. This makes them suitable for containers.
  • Shared_Ptr automatically calls delete for the wrapped pointer when its last copy goes out of scope.
  • Shared_Ptr simplifies execution safety.

Without smart pointers, it can be quite challenging to ensure that every dynamically allocated pointer (i.e. use of new) is deleted in the event of all possible exceptions. In addition to the exceptions we throw ourselves, "new" itself will throw an exception it it fails, as does the STL (Standard Template Library which includes vector and string). Without smart pointers we often need to resort to additional try/catch blocks simply to avoid memory leaks when exception occur.

Examples of declaring a Shared_Ptr:

Shared_Ptr<CWnd> w1(new CWnd);
Shared_Ptr<CWnd> w1 = new CWnd;
typedef Shared_Ptr<CWnd> CWndPtr;
CWndPtr w1 = new CWnd;
typedef Shared_Ptr<CWnd> CWndPtr;
CWndPtr w1(new CWnd);

Examples using a Shared_Ptr in a vector:

typedef Shared_Ptr<CWnd> CWndPtr;
std::vector<CWndPtr> MyVector;
MyVector.push_back(new CWnd);
typedef Shared_Ptr<CWnd> CWndPtr;
CWnd* pWnd = new CWnd;
std::vector<CWndPtr> MyVector;
MyVector.push_back(pWnd);

Summary Information

Header file shared_ptr.h
Win32/64 support Yes
WinCE support Yes