Listing 4: A class that can throw exceptions from any possible user-defined function
#ifndef TESTCLASS_H #define TESTCLASS_H #include "Counter.h" class TestClass { public: TestClass( int inVal = 0 ) : mVal( inVal ) { Counter::CouldThrow(); } TestClass( const TestClass& other ) : mVal( other.mVal ) { Counter::CouldThrow(); } ~TestClass() {} TestClass& operator=( const TestClass& other ) { Counter::CouldThrow(); mVal = other.mVal; return *this; } bool operator==( const TestClass& other ) { Counter::CouldThrow(); return other.mVal == this->mVal; } bool operator!=( const TestClass& other ) { Counter::CouldThrow(); return other.mVal != this->mVal; } bool operator<( const TestClass& other ) { Counter::CouldThrow(); return other.mVal < this->mVal; } bool operator<=( const TestClass& other ) { Counter::CouldThrow(); return other.mVal <= this->mVal; } // not shown: other operators // ... // Don't need to overload class operator new, // because we already required that the global // operator new be overloaded to call // Counter::CouldThrow(). // See online version of this listing for discussion of // remaining recommended functions to be implemented private: int mVal; }; // I have considered that it is not really desireable to add any // more functionality to this class which might result in silent // automatic conversions etc. If you need a particular method, // you may wish to add it. #endif // TESTCLASS_H End of Listing