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

  1. //: C08:Castaway.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. // "Casting away" constness
  7.  
  8. class Y {
  9.   int i;
  10. public:
  11.   Y();
  12.   void f() const;
  13. };
  14.  
  15. Y:: Y() { i = 0; }
  16.  
  17. void Y::f() const {
  18. //!    i++; // Error -- const member function
  19.     ((Y*)this)->i++; // OK: cast away const-ness
  20. }
  21.  
  22. int main() {
  23.   const Y yy;
  24.   yy.f(); // Actually changes it!
  25. } ///:~
  26.