C ++ virtual function table (2)

Source: Internet
Author: User
# 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.
 
 
 
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.