fork(2) download
  1. #include <iostream>
  2.  
  3. class X
  4. {
  5. public:
  6. X() : private_(1) { /*...*/ }
  7.  
  8. private:
  9. int Value() { return private_; }
  10. int private_;
  11. };
  12.  
  13. // Nasty attempt to simulate the object layout
  14. // (cross your fingers and toes).
  15. //
  16. class BaitAndSwitch
  17. // hopefully has the same data layout as X
  18. { // so we can pass him off as one
  19. public:
  20. int Value() { return private_; }
  21. private:
  22. int private_;
  23. };
  24.  
  25. int f( X& x )
  26. {
  27. // evil laughter here
  28. (reinterpret_cast<BaitAndSwitch&>(x)).Value();
  29. }
  30.  
  31. int main()
  32. {
  33. X x;
  34. std::cout << f(x) << "\n"; // prints 0, not 1
  35. return 0;
  36. };
Success #stdin #stdout 0s 2928KB
stdin
Standard input is empty
stdout
0