boost::shared_ptr is a type of shared_ptr (which is a type of smart pointer) that can be copied safely and cheap, without copying the object pointed to. When the last boost::shared_ptr using an object goes out of scope, it will delete the object pointedto.
Creating a boost::shared_ptr
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/boost::shared_ptr.hpp> struct Test { Test(const int x) : m_x(x) {} const int m_x; }; int main() { boost::shared_ptr<Test> p; p->m_x; //Good: uninitialized pointer detected by Boost }
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/boost::shared_ptr.hpp> struct Test { Test(const int x) : m_x(x) {} const int m_x; }; Test * CreateNullPointer() { return 0; } int main() { boost::shared_ptr<Test> p; //p.reset(0); //Good: does not compile: 0 is an integer //p.reset(NULL); //Good: does not compile p.reset(CreateNullPointer()); //Bad: tricked the compiler //p->m_x; //Good: uninitialized pointer detected by Boost }
Smart pointers and ==
#include <cassert> #include <boost/boost::shared_ptr.hpp> ///A test class struct Test { int m_x; }; ///Class to manage a Test struct TestManager { ///Construct an initialized Test TestManager() : m_test(new Test) { m_test->m_x = 1; } ///Obtain a read-only boost::shared_ptr to the Test managed const boost::shared_ptr<const Test> GetTest() const { return m_test; } ///Obtain a read-and-write boost::shared_ptr to the Test managed const boost::shared_ptr<Test> GetTest() { return m_test; } private: ///The Test managed boost::shared_ptr<Test> m_test; }; int main() { TestManager m; assert(m.GetTest()->m_x == 1); //Read from m m.GetTest()->m_x = 2; //Write to m assert(m.GetTest()->m_x == 2); //Read from m const boost::shared_ptr<const Test> v(m.GetTest()); assert(m.GetTest() == v); //boost::shared_ptr's to same object are evaluated to true by operator== //v->m_x = 3; //Does not compile: v holds a read-only pointer const boost::shared_ptr<Test> w(boost::const_pointer_cast<Test>(v)); assert(m.GetTest() == w); //boost::shared_ptr's to same object are evaluated to true by operator== assert(v == w); //boost::shared_ptr's to same object are evaluated to true by operator== w->m_x = 3; //Does compile assert(v->m_x == 3); assert(w->m_x == 3); assert(m.GetTest()->m_x == 3); }
Cast from boost::shared_ptr<const Test> to boost::shared_ptr<Test>
#include <cassert> #include <boost/boost::shared_ptr.hpp> ///A test class struct Test { int m_x; }; ///Class to manage a Test struct TestManager { ///Construct an initialized Test TestManager() : m_test(new Test) { m_test->m_x = 1; } ///Obtain a read-only boost::shared_ptr to the Test managed const boost::shared_ptr<const Test> GetTest() const { return m_test; } ///Obtain a read-and-write boost::shared_ptr to the Test managed const boost::shared_ptr<Test> GetTest() { return m_test; } private: ///The Test managed boost::shared_ptr<Test> m_test; }; int main() { TestManager m; assert(m.GetTest()->m_x == 1); //Read from m m.GetTest()->m_x = 2; //Write to m assert(m.GetTest()->m_x == 2); //Read from m const boost::shared_ptr<const Test> v(m.GetTest()); assert(m.GetTest() == v); //boost::shared_ptr's to same object are evaluated to true by operator== //v->m_x = 3; //Does not compile: v holds a read-only pointer const boost::shared_ptr<Test> w(boost::const_pointer_cast<Test>(v)); assert(m.GetTest() == w); //boost::shared_ptr's to same object are evaluated to true by operator== assert(v == w); //boost::shared_ptr's to same object are evaluated to true by operator== w->m_x = 3; //Does compile assert(v->m_x == 3); assert(w->m_x == 3); assert(m.GetTest()->m_x == 3); }