The Boost C++ Libraries

Sole Ownership

boost::scoped_ptr is a smart pointer that is the sole owner of a dynamically allocated object. boost::scoped_ptr cannot be copied or moved. This smart pointer is defined in the header file boost/scoped_ptr.hpp.

Example 1.1. Using boost::scoped_ptr
#include <boost/scoped_ptr.hpp>
#include <iostream>

int main()
  boost::scoped_ptr<int> p{new int{1}};
  std::cout << *p << '\n';
  p.reset(new int{2});
  std::cout << *p.get() << '\n';
  std::cout << std::boolalpha << static_cast<bool>(p) << '\n';

A smart pointer of type boost::scoped_ptr can’t transfer ownership of an object. Once initialized with an address, the dynamically allocated object is released when the destructor is executed or when the member function reset() is called.

Example 1.1 uses a smart pointer p with the type boost::scoped_ptr<int>. p is initialized with a pointer to a dynamically allocated object that stores the number 1. Via operator*, p is de-referenced and 1 written to standard output.

With reset() a new address can be stored in the smart pointer. That way the example passes the address of a newly allocated int object containing the number 2 to p. With the call to reset(), the currently referenced object in p is automatically destroyed.

get() returns the address of the object anchored in the smart pointer. The example de-references the address returned by get() to write 2 to standard output.

boost::scoped_ptr overloads the operator operator bool. operator bool returns true if the smart pointer contains a reference to an object – that is, if it isn’t empty. The example writes false to standard output because p has been reset with a call to reset().

The destructor of boost::scoped_ptr releases the referenced object with delete. That’s why boost::scoped_ptr must not be initialized with the address of a dynamically allocated array, which would have to be released with delete[]. For arrays, Boost.SmartPointers provides the class boost::scoped_array.

Example 1.2. Using boost::scoped_array
#include <boost/scoped_array.hpp>

int main()
  boost::scoped_array<int> p{new int[2]};
  *p.get() = 1;
  p[1] = 2;
  p.reset(new int[3]);

The smart pointer boost::scoped_array is used like boost::scoped_ptr. The crucial difference is that the destructor of boost::scoped_array uses the operator delete[] to release the contained object. Because this operator only applies to arrays, a boost::scoped_array must be initialized with the address of a dynamically allocated array.

boost::scoped_array is defined in boost/scoped_array.hpp.

boost::scoped_array provides overloads for operator[] and operator bool. Using operator[], a specific element of the array can be accessed. Thus, an object of type boost::scoped_array behaves like the array it owns. Example 1.2 saves the number 2 as the second element in the array referred to by p.

Like boost::scoped_ptr, the member functions get() and reset() are provided to retrieve and reinitialize the address of the contained object.