home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / C++-7 / DISK4 / SAMPLES / CPPTUTOR / NEWDEL4.CP$ / NEWDEL4
Encoding:
Text File  |  1991-12-12  |  1.1 KB  |  45 lines

  1. // NEWDEL4.CPP
  2.  
  3. // This is an example program from Chapter 6 of the C++ Tutorial. This
  4. //     program demonstrates class-specific new and delete operators
  5. //     with constructor and destructor messages to indicate when they
  6. //     are called.
  7.  
  8. #include <malloc.h>
  9. #include <iostream.h>
  10.  
  11. class Name
  12. {
  13. public:
  14.     Name()  { cout << "Name's constructor running\n"; }
  15.     void *operator new( size_t size );
  16.     void operator delete( void *ptr );
  17.     ~Name() { cout << "Name's destructor running\n"; }
  18. private:
  19.     char name[25];
  20. };
  21.  
  22. // -------- Simple memory pool to handle one Name
  23. char pool[sizeof( Name )];
  24.  
  25. // -------- Overloaded new operator for the Name class
  26. void *Name::operator new( size_t size )
  27. {
  28.     cout << "Name's new running\n";
  29.     return pool; 
  30. }
  31.  
  32. // --------- Overloaded delete operator for the Name class
  33. void Name::operator delete( void *p )
  34. {
  35.     cout << "Name's delete running\n";
  36. }
  37.  
  38. void main()
  39. {
  40.     cout << "Executing: nm = new Name\n";
  41.     Name *nm = new Name;
  42.     cout << "Executing: delete nm\n";
  43.     delete nm;
  44. }
  45.