# Include <iostream> # include <tchar. h> using namespace STD; Class human {public: Virtual void print () {cout <_ T ("Human: Print") <Endl;} virtual void walk () {cout <_ T ("Human: Walk") <Endl ;}}; int _ tmain (INT argc, tchar argv [], tchar envp []) {typedef void (* func) (); Human Modi; int * pmodi = (int *) (& Modi); int * addrofmodivtable = (int *) (* pmodi ); cout <_ T ("first address of the memory where the Modi virtual function table is located:") <addrofmodivtable <Endl; func pfun = (func) (* addrofmodivtable ); pfun (); pfun = (func) (* (++ addrofmodivtable); pfun (); int nend = (INT) * (++ addrofmodivtable ); cout <nend <Endl; return 0 ;}
How can I access a virtual function through a virtual function table?
The running result is as follows:
Func pfun = (func) (* addrofmodivtable); Detailed explanation:
* Addrofmodivtable: retrieves the first four bytes of the memory of the Modi virtual function table.
(Func) (* addrofmodivtable): forcibly converts the type of content A from int to void (*)().
(Func) (* (addrofmodivtable ++): retrieves the fifth to eighth bytes stored in the memory of the Modi virtual function table B, the content B type is forcibly converted from int to void (*)().
The value of nend is zero, indicating that the content of the third item in the virtual function table is null, which serves as the end mark of the virtual function table.