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

  1. // \EXAMPLES\EX0910.CPP
  2. // demonstration of overloaded new operator
  3. //
  4. //------------------------------------------------------------
  5. // The Microsoft Visual C++ compiler has a different syntax
  6. //     for customizing the new operator.
  7. // This example does not compile in Visual C++.
  8. //------------------------------------------------------------
  9.  
  10. // Files used in this example:
  11. //------------------------------------------------------------
  12. // %F,15,EX09101.H%EX09101.H
  13. // %F,15,EX09101.CPP%EX09101.CPP
  14. // EX0910.CPP      this file
  15. //------------------------------------------------------------
  16.  
  17. #include <iostream.h>
  18. #include "EX09101.H"
  19.  
  20. void main()
  21. {
  22.  
  23.    // points to the head of the list
  24.    CachedNode* classList = new CachedNode;
  25.  
  26.    // used to point to a node in the list
  27.    CachedNode* pClass = classList;
  28.  
  29.    // allocate 3 more nodes and add them to the list
  30.    // the last node will be allocated using ::new
  31.    for (int i = 1; i <= 3; i++)
  32.    {
  33.       pClass->Next(new CachedNode);// create and add a node
  34.       pClass = pClass->Next();     // point to new node
  35.    }
  36.  
  37.    // deallocate all the elements in the list
  38.    // Notice that the last node is deallocated using ::new
  39.    for (int j = 1; j <= 4; j++)
  40.    {
  41.       pClass = classList;         // point to first node
  42.       classList = pClass->Next(); // removed first node
  43.       delete pClass;              // delete node
  44.    }
  45. }
  46.  
  47.