Created
December 21, 2022 00:15
-
-
Save lexuanquynh/a28671beb870444cf3cfb2eeb6f71843 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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