fork download
  1. #include <iostream>
  2. #include <cstdio>
  3.  
  4. using namespace std;
  5.  
  6. class A {
  7. public:
  8. virtual void func() { cout << "i: " << i_ << endl; }
  9. void inc() { ++i_; }
  10. A() { i_=10; }
  11. private:
  12. int i_;
  13. };
  14.  
  15. int main() {
  16. void (A::*p)() = &A::func;
  17. int size = sizeof(p)/sizeof(unsigned int);
  18. cout << "size: " << size << endl;
  19. unsigned int *t = new unsigned int[size];
  20. for (int i = 0; i < size; ++i) t[i] = ((unsigned int *)&p)[i];
  21. void (A::*q)() = *(void (A::**)())t;
  22. delete [] t;
  23.  
  24. A a;
  25. (a.*q)();
  26. a.inc();
  27. (a.*q)();
  28.  
  29. A aa;
  30. (aa.*q)();
  31. (a.*q)();
  32. (aa.*q)();
  33.  
  34. return 0;
  35. }
  36.  
  37.  
  38.  
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
size: 2
i: 10
i: 11
i: 10
i: 11
i: 10