Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
shared_ptr is a type of smart pointer that can be copied safely and cheap, without copying the object pointed to. When the last shared_ptr using an object goes out of scope, it will delete the object pointedto.
//Create a Widget |
The shared_ptr on this page has multiple names:

boost::shared_ptr:
from the Boost library (under C++98):
#include <boost/shared_ptr.hpp> |

std::tr1::shared_ptr:
from the C++98 its tr1 library:
#include <tr1/memory> |

std::shared_ptr:
from the C++0x its STL library:
#include <memory> |
Smart pointers and null
Boost smart pointers check for null themselves, so there is no need to check these to be inititialized. In the example below a member variable of a class is requested from an unitialized smart pointer. The program will abort and the runtime error will be shown.
#include <boost/shared_ptr.hpp> |
The code below shows that initializing a boost::shared_ptr with null will not be easy, but even when it succeeds, boost::shared_ptr will check itself for null.
#include <boost/shared_ptr.hpp> |
Smart pointers and ==
#include <cassert> |
Cast from boost::shared_ptr<const Test> to boost::shared_ptr<Test>
#include <cassert> |
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.