home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C22 / Mithis.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.3 KB  |  67 lines

  1. //: C22:Mithis.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. // MI and the "this" pointer
  7. #include <fstream>
  8. using namespace std;
  9. ofstream out("mithis.out");
  10.  
  11. class Base1 {
  12.   char c[0x10];
  13. public:
  14.   void printthis1() {
  15.     out << "Base1 this = " << this << endl;
  16.   }
  17. };
  18.  
  19. class Base2 {
  20.   char c[0x10];
  21. public:
  22.   void printthis2() {
  23.     out << "Base2 this = " << this << endl;
  24.   }
  25. };
  26.  
  27. class Member1 {
  28.   char c[0x10];
  29. public:
  30.   void printthism1() {
  31.     out << "Member1 this = " << this << endl;
  32.   }
  33. };
  34.  
  35. class Member2 {
  36.   char c[0x10];
  37. public:
  38.   void printthism2() {
  39.     out << "Member2 this = " << this << endl;
  40.   }
  41. };
  42.  
  43. class MI : public Base1, public Base2 {
  44.   Member1 m1;
  45.   Member2 m2;
  46. public:
  47.   void printthis() {
  48.     out << "MI this = " << this << endl;
  49.     printthis1();
  50.     printthis2();
  51.     m1.printthism1();
  52.     m2.printthism2();
  53.   }
  54. };
  55.  
  56. int main() {
  57.   MI mi;
  58.   out << "sizeof(mi) = "
  59.     << hex << sizeof(mi) << " hex" << endl;
  60.   mi.printthis();
  61.   // A second demonstration:
  62.   Base1* b1 = &mi; // Upcast
  63.   Base2* b2 = &mi; // Upcast
  64.   out << "Base 1 pointer = " << b1 << endl;
  65.   out << "Base 2 pointer = " << b2 << endl;
  66. } ///:~
  67.