home *** CD-ROM | disk | FTP | other *** search
- // \EXAMPLES\EX09101.H
-
- // Demonstration of overloaded new and delete operators.
-
- // This program does not work on Microsoft Visual C++
- // because that compiler has a different implementation
- // of the new and delete operators.
-
- // Files used in this example:
- //---------------------------------------------------
- // EX09101.H this file
- // %F,15,EX09101.CPP%EX09101.CPP
- // %F,15,EX0910.CPP%EX0910.CPP
-
- class CachedNode;
- //---------------------------------------------------------
- // Class: CachedNode
- // This class shows you how to overload the new operator
- //---------------------------------------------------------
- class CachedNode
- {
- static CachedNode* nextFree; // The first available free node
- static long cacheSize; // The size of the cache
- static long numUsed; // The number of used elements
- static CachedNode* pCache; // Pointer to the cache
-
- CachedNode* next; // Pointer to the next node
- long data; // The node's data
-
- public:
-
- //------------------------------------------------------
- // FUNTION: new() is overloaded to make use of a cache
- // PARAMETERS: p is a pointer to the Node to be deleted
- // bytesUsed is the size of the Node
- // RETURN VALUE: an object of type CachedNode
- //------------------------------------------------------
- static void*
- operator new( size_t bytesReq);
-
- //------------------------------------------------------
- // FUNTION: delete() is overloaded to make use of a cache
- // PARAMETERS: p is a pointer to the Node to be deleted
- // bytesUsed is the size of the Node
- //------------------------------------------------------
- static void
- operator delete( void* p, size_t bytesUsed);
-
- //------------------------------------------------------
- // FUNTION: IniCache() initializes the cache
- // RETURN VALUE: a pointer to a cache
- //------------------------------------------------------
- static CachedNode*
- CachedNode::IniCache();
-
- //------------------------------------------------------
- // FUNTION:
- // RETURN VALUE: a pointer the next node
- //------------------------------------------------------
- CachedNode*
- Next();
-
- //------------------------------------------------------
- // FUNTION: Next assigns the next node
- // PARAMETERS: p the next node
- //------------------------------------------------------
- void
- Next( CachedNode* p);
-
- //------------------------------------------------------
- // FUNTION: Data()
- // RETURN VALUE: the data stored in a node
- //------------------------------------------------------
- long
- Data();
-
- //------------------------------------------------------
- // FUNTION: Data() assigns data to the node
- // PARAMETERS: datum, the new data
- //------------------------------------------------------
- void
- Data( long datum);
- };