Skip to content

Instantly share code, notes, and snippets.

@lexuanquynh
Created December 21, 2022 00:21
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/b38182a42fad079803f913dcb867b470 to your computer and use it in GitHub Desktop.
Save lexuanquynh/b38182a42fad079803f913dcb867b470 to your computer and use it in GitHub Desktop.
#include < iostream >
#include < string >
#include < cstring >
class Sample1 {
public:
virtual void vMethod1() {
printf("This is virtual method 1\n");
printf("Sample1: member1 = %d\n", member1);
}
virtual void vMethod2() {
printf("Sample1: This is virtual method 2\n");
}
void method2() {
printf("This is NONVIRTUAL method 2\n");
}
public:
int member1;
int member2;
};
class Sample2: public Sample1 {
public:
//override vMethod1()
virtual void vMethod1() {
printf("Sample2: This is virtual method 1\n");
}
virtual void vMethod3() {
printf("Sample2: This is virtual method 3\n");
}
};
//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();
MyFunc *virtualMethodTable = (MyFunc*)(*(MyFunc*)a);
Sample2 *b = new Sample2();
MyFunc* virtualMethodTable2;
memcpy(&virtualMethodTable2, b, 8);
printf("\n\n");
printf("Sample1 - vMethod1 Addr: %p\n", virtualMethodTable[0]);
printf("Sample1 - vMethod2 Addr: %p\n", virtualMethodTable[1]);
printf("--------------------------------\n");
printf("Sample2 - vMethod1 Addr: %p\n", virtualMethodTable2[0]);
printf("Sample2 - vMethod2 Addr: %p\n", virtualMethodTable2[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment