/****************************************************************************** *** FILE: error_handler.cpp *** DATE: 03/17/2004 *** AUTHOR: Daniel Dementiev *** GOAL: This program illustrates how to use user defined error handlers. *** Line: *** typedef void (*error_handler_ptr)(char*) *** defines a new type 'error_handler_ptr' that is a pointer to a *** function that takes one argument of the char* type and returns *** no value (void). *** Class Array has a private member err_handler of this new type, *** and this member is used in the method Item(): *** (*err_handler)("argument") ******************************************************************************/ #include #include using namespace std; class Array { private: int count; int data[10]; int size; typedef void (*error_handler_ptr)(char*); error_handler_ptr err_handler; public: Array() : count(0), size(10), err_handler(NULL) {}; int Count() const { return count; }; int Size() const { return size; }; int& Item(int index); void SetErrorHandler(error_handler_ptr err) { err_handler = err; }; }; int& Array::Item(int index) { if( (index<0 || index>=10) && err_handler!=NULL ) (*err_handler)("Index is out of range in Array::Item() method"); return data[index]; } // this function will be invoked if any error occurs in the Array class methods void MyErrFunction(char *msg) { cerr<<"Error: "<