home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_13_07 / saks / noeval.cpp < prev   
Encoding:
C/C++ Source or Header  |  1995-05-02  |  1.1 KB  |  65 lines

  1. Listing 1 - A program the illustrates the effect of 
  2. when a program does not evaluate the left operand of a 
  3. member access expression
  4.  
  5. #include <iostream.h>
  6.  
  7. class widget
  8.     {
  9. public:
  10.     widget(int i);
  11.     ~widget();
  12.     static unsigned how_many();
  13.     int value() const;
  14. private:
  15.     int v;
  16.     static unsigned counter;
  17.     };
  18.  
  19. widget::widget(int i)
  20.     : v(i)
  21.     {
  22.     ++counter;
  23.     }
  24.  
  25. widget::~widget()
  26.     {
  27.     --counter;
  28.     }
  29.  
  30. unsigned widget::how_many()
  31.     {
  32.     return counter;
  33.     }
  34.  
  35. int widget::value() const
  36.     {
  37.     return v;
  38.     }
  39.  
  40. unsigned widget::counter = 0;
  41.  
  42. #define DIM(a) (sizeof(a)/sizeof(a[0]))
  43. #define BEYOND(a) (a + DIM(a))
  44.  
  45. int main()
  46.     {
  47.     int i;
  48.     widget *a[10];
  49.     for (i = 0; i < DIM(a); ++i)
  50.         a[i] = new widget (i * i);
  51.     widget **p = a;
  52.     while (p < BEYOND(a))
  53.         {
  54.         cout << (*p)->how_many() << ": ";
  55.         cout << (*p++)->value() << endl;
  56.         }
  57.     while (p > a)
  58.         {
  59.         cout << (*--p)->how_many() << ": ";
  60.         cout << (*p)->value() << endl;
  61.         delete *p;
  62.         }
  63.     return 0;
  64.     }
  65.