Appendix I. Source Code for the file scpp_ptr.hpp

#ifndef __SCPP_PTR_HPP_INCLUDED__
#define __SCPP_PTR_HPP_INCLUDED__

#include "scpp_assert.hpp"

namespace scpp {

// Template pointer, does not take ownership of an object.
template <typename T>
class Ptr {
  public:

  explicit Ptr(T* p = NULL)
  : ptr_(p) {
  }

  T* Get() const {
    return ptr_;
  }

  Ptr<T>& operator=(T* p) {
    ptr_ = p;
    return *this;
  }

  T* operator->() const {
    SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator -> on NULL pointer.");
    return ptr_;
  }

  T& operator* () const {
    SCPP_TEST_ASSERT(ptr_ != NULL, "Attempt to use operator * on NULL pointer.");
    return *ptr_;
  }

private:
  T*  ptr_;
};

} // namespace scpp

#endif // __SCPP_PTR_HPP_INCLUDED__

Get Safe C++ now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.