home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C03 / Union.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  682 b   |  30 lines

  1. //: C03:Union.cpp
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // The size and simple use of a union
  7. #include <iostream>
  8. using namespace std;
  9.  
  10. union packed { // Declaration similar to a class
  11.   char i;
  12.   short j;
  13.   int k;
  14.   long l;
  15.   float f;
  16.   double d;  
  17.   // The union will be the size of a 
  18.   // double, since that's the largest element
  19. };  // Semicolon ends a union, like a struct
  20.  
  21. int main() {
  22.   cout << "sizeof(packed) = " 
  23.     << sizeof(packed) << endl;
  24.   packed x;
  25.   x.i = 'c';
  26.   cout << x.i << endl;
  27.   x.d = 3.14159;
  28.   cout << x.d << endl;
  29. } ///:~
  30.