Skip to content

Instantly share code, notes, and snippets.

@lexuanquynh
Created December 21, 2022 00:18
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/59b8e9e3c22bc137f04dabf5289227c2 to your computer and use it in GitHub Desktop.
Save lexuanquynh/59b8e9e3c22bc137f04dabf5289227c2 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");
printf("member1 = %d\n", member1);
}
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
//Thêm tham số thisPtr ở đây
typedef void (*MyFunc)(Sample1 *thisPtr);
int main(int argc, const char * argv[]) {
Sample1 *a = new Sample1();
a->member1 = 1000;
printf("size of Sample1: %d\n", sizeof(Sample1));
MyFunc *virtualMethodTable = (MyFunc*)(*(MyFunc*)a);
//Lệnh trên tương đương với:
// memcpy(&virtualMethodTable2, b, 8);
virtualMethodTable[0](a); //call method1
virtualMethodTable[1](a); //call method2
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment