fork download
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. struct A
  7. {
  8. virtual void func()
  9. {
  10. printf("i: %d\n", i_);
  11. }
  12. void inc()
  13. {
  14. ++i_;
  15. }
  16. A()
  17. {
  18. i_=10;
  19. }
  20. private:
  21. int i_;
  22. };
  23.  
  24.  
  25. int main()
  26. {
  27. void (A::*p)() = &A::func;
  28.  
  29. A a;
  30. (a.*p)();
  31.  
  32. unsigned int vptr_addr = *(unsigned int *)&a;
  33. cout << "vptr_addr:" << hex << vptr_addr << endl;
  34.  
  35. unsigned int func_addr = *(unsigned int *)vptr_addr;
  36. cout << "func_addr:" << hex << func_addr << endl;
  37. a.inc();
  38. (*(void(*)(A*))(func_addr) )(&a);
  39.  
  40. A aa;
  41. (*(void(*)(A*))(func_addr) )(&aa);
  42.  
  43. (*(void(*)(A*))(func_addr) )(&a);
  44.  
  45. (*(void(*)(A*))(func_addr) )(&aa);
  46.  
  47. return 0;
  48. }
  49.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
i: 10
vptr_addr:8048958
func_addr:8048870
i: 11
i: 10
i: 11
i: 10