home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / IDIOMS.ZIP / 3-19.C < prev    next >
C/C++ Source or Header  |  1991-12-04  |  1KB  |  41 lines

  1. /* Copyright (c) 1992 by AT&T Bell Laboratories. */
  2. /* Advanced C++ Programming Styles and Idioms */
  3. /* James O. Coplien */
  4. /* All rights reserved. */
  5.  
  6. #include <iostream.h>
  7. #include <stddef.h>
  8.  
  9. void *operator new(size_t, void *p) { return p; }
  10.  
  11. class Foo {
  12. public:
  13.     Foo() { rep = 1; }
  14.     Foo(int i) { rep = i; }
  15.     ~Foo() { rep = 0; }
  16.     void print() { cout << rep << "\en"; }
  17. private:
  18.     int rep;
  19. };
  20.  
  21. struct { int:0; };  // machine-dependent alignment
  22. char foobar[sizeof(Foo)];
  23.  
  24. Foo foefum;
  25.  
  26. int main()
  27. {
  28.     foefum.print();
  29.     (&foefum)->Foo::~Foo();   // cause premature cleanup of
  30.                               // object with global extent
  31.     foefum.print();           // undefined results!
  32.     Foo *fooptr = new(foobar) Foo;
  33.     fooptr->Foo::~Foo();
  34.     fooptr = new(foobar) Foo(1);   // unrelated to earlier
  35.                                    //   allocation
  36.     fooptr->Foo::~Foo();      // cause premature cleanup
  37.                               // do NOT delete fooptr
  38.     fooptr = new Foo;
  39.     delete fooptr;
  40. }
  41.