fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class foo {
  5. int value;
  6. static int static_value;
  7.  
  8. public:
  9.  
  10. static void set_value (void *ptr, int val) {
  11. reinterpret_cast<foo*>(ptr)->value = val;
  12. }
  13. int get() const { return value; }
  14.  
  15. static void set_static_value (void *ptr, int val) {
  16. reinterpret_cast<foo*>(ptr)->static_value = val;
  17. // объект излишен, т.к. меняем статический член
  18. // достаточно static_value = val;
  19. }
  20. static int get_static() { return static_value; }
  21.  
  22. };
  23.  
  24. int foo::static_value;
  25.  
  26. int main() {
  27.  
  28. foo bar;
  29. foo::set_value (&bar, 123);
  30. cout << bar.get();
  31.  
  32. foo::set_static_value (&bar, 456);
  33. cout << bar.get_static();
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
123456