Skip to content

Instantly share code, notes, and snippets.

@lexuanquynh
Created December 21, 2022 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lexuanquynh/a28671beb870444cf3cfb2eeb6f71843 to your computer and use it in GitHub Desktop.
Save lexuanquynh/a28671beb870444cf3cfb2eeb6f71843 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
class Sample1 {
public:
virtual void vMethod1() {
printf("This is virtual method 1\n");
}
virtual void vMethod2() {
printf("This is virtual method 2\n");
}
void method2() {
printf("This is NONVIRTUAL method 2\n");
}
public:
int member1;
int member2;
};
//Define method type
typedef void (*MyFunc)();
int main(int argc, const char * argv[]) {
Sample1 *a = new Sample1();
printf("size of Sample1: %d\n", sizeof(Sample1));
MyFunc *virtualMethodTable = (MyFunc*)(*(MyFunc*)a);
virtualMethodTable[0](); //call method1
virtualMethodTable[1](); //call method2
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment