home *** CD-ROM | disk | FTP | other *** search
/ Microsoftware Monthly 19…2 Programming Power Tools / MASO9512.ISO / cpptutor / cpptutor.arj / EXAMPLES / EX09101.H < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-27  |  2.9 KB  |  84 lines

  1. // \EXAMPLES\EX09101.H
  2.  
  3. // Demonstration of overloaded new and delete operators.
  4.  
  5. // This program does not work on Microsoft Visual C++
  6. //    because that compiler has a different implementation
  7. //    of the new and delete operators.
  8.  
  9. // Files used in this example:
  10. //---------------------------------------------------
  11. // EX09101.H       this file
  12. // %F,15,EX09101.CPP%EX09101.CPP
  13. // %F,15,EX0910.CPP%EX0910.CPP
  14.  
  15. class CachedNode;
  16. //---------------------------------------------------------
  17. // Class: CachedNode
  18. //        This class shows you how to overload the new operator
  19. //---------------------------------------------------------
  20. class CachedNode
  21. {
  22.    static CachedNode* nextFree;  // The first available free node
  23.    static long cacheSize;        // The size of the cache
  24.    static long numUsed;          // The number of used elements
  25.    static CachedNode* pCache;    // Pointer to the cache
  26.  
  27.    CachedNode* next;             // Pointer to the next node
  28.    long        data;             // The node's data
  29.  
  30. public:
  31.  
  32.    //------------------------------------------------------
  33.    // FUNTION: new() is overloaded to make use of a cache
  34.    // PARAMETERS: p is a pointer to the Node to be deleted
  35.    //             bytesUsed is the size of the Node
  36.    // RETURN VALUE: an object of type CachedNode
  37.    //------------------------------------------------------
  38.    static void*
  39.    operator new( size_t bytesReq);
  40.  
  41.    //------------------------------------------------------
  42.    // FUNTION: delete() is overloaded to make use of a cache
  43.    // PARAMETERS: p is a pointer to the Node to be deleted
  44.    //             bytesUsed is the size of the Node
  45.    //------------------------------------------------------
  46.    static void
  47.    operator delete( void* p, size_t bytesUsed);
  48.  
  49.    //------------------------------------------------------
  50.    // FUNTION: IniCache() initializes the cache
  51.    // RETURN VALUE: a pointer to a cache
  52.    //------------------------------------------------------
  53.    static CachedNode*
  54.    CachedNode::IniCache();
  55.  
  56.    //------------------------------------------------------
  57.    // FUNTION:
  58.    // RETURN VALUE: a pointer the next node
  59.    //------------------------------------------------------
  60.    CachedNode*
  61.    Next();
  62.  
  63.    //------------------------------------------------------
  64.    // FUNTION: Next assigns the next node
  65.    // PARAMETERS: p the next node
  66.    //------------------------------------------------------
  67.    void
  68.    Next( CachedNode* p);
  69.  
  70.    //------------------------------------------------------
  71.    // FUNTION: Data()
  72.    // RETURN VALUE: the data stored in a node
  73.    //------------------------------------------------------
  74.    long
  75.    Data();
  76.  
  77.    //------------------------------------------------------
  78.    // FUNTION: Data() assigns data to the node
  79.    // PARAMETERS: datum,  the new data
  80.    //------------------------------------------------------
  81.    void
  82.    Data( long datum);
  83. };
  84.