// AbsPtr.hpp #ifndef AbsPtr_H #define AbsPtr_H // Abstract base for a pointer class. // The template class Ptr meets the interface of but does not inherit // from AbsPtr to minimize its size and maximize its speed. // // Concrete subclasses implement the method pointer. // // Exception PtrInvalidPointer is thrown if user attempts dereference // or member selection with a null pointer. #include "ptr/PtrException.h" template class AbsPtr { public: // Destructor. ~AbsPtr() { } // Return the pointer to the object. // Return 0 if the pointer is not valid. virtual T* pointer() const =0; // dereference T& operator*() const { T* ptr = pointer(); if ( ! ptr ) throw PtrInvalidPointer(); return *ptr; } // class member selection T* operator->() const { T* ptr = pointer(); if ( ! ptr ) throw PtrInvalidPointer(); return ptr; } // Conversion to void*. // This enables comparisons (==, <, ...) and output stream. operator const void*() const { return pointer(); } }; #endif