fork download
  1. #include <iostream>
  2.  
  3. class Test{
  4. private:
  5. int a;
  6. public:
  7. Test(int i, double j) : a{i}, b{j} {}
  8. void show();
  9. double b;
  10. };
  11.  
  12. void Test::show(){
  13. std::cout << a << " " << b << std::endl;
  14. }
  15.  
  16. void doMagic(Test& x){
  17. Test* objPtr = &x;
  18. int* val1Ptr = reinterpret_cast<int*>(objPtr);
  19. *val1Ptr = 41;
  20. double* val2Ptr = reinterpret_cast<double*>(val1Ptr+1);
  21. *val2Ptr = 2.71;
  22. }
  23.  
  24. int main(){
  25. Test x(1, 3.14);
  26. std::cout << "Przed modyfikacja: ";
  27. x.show();
  28. doMagic(x);
  29. std::cout << "Po modyfikacji: ";
  30. x.show();
  31. return 0;
  32. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
Przed modyfikacja: 1 3.14
Po modyfikacji: 41 2.71