fork download
  1. #include<iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. class B;
  6. typedef void (*HANDLE_DOSOMETHING)(B *const, int q);
  7.  
  8. class B
  9. {
  10. public:
  11. virtual void doSomething(int q)
  12. {
  13. std::cout<<"B::doSomething()"<<q<<endl;
  14. }
  15. void dummy()
  16. {
  17. HANDLE_DOSOMETHING *f1ptr = NULL;
  18. int *vtbl = NULL;
  19. int *vptr = (int *)this; // address of the object
  20.  
  21. vtbl = (int *)*vptr; //address of the VTABLE
  22.  
  23. f1ptr = (HANDLE_DOSOMETHING *)&(vtbl[0]); //address of the 1st virtual function
  24. (*f1ptr)(this, 55);
  25. }
  26. };
  27. int main()
  28. {
  29. B objb;
  30. objb.dummy();
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
B::doSomething()55