Computer _ virtual function table memory distribution (Chen Hao's)

Source: Internet
Author: User
Original article link: Workshop. Lab
Class base {public: Virtual void F () {cout <"base: F ()" <Endl;} virtual void g () {cout <"base :: G () "<Endl;} virtual void H () {cout <" base: H () "<Endl;} protected: Private :}; class base2 {public: Virtual void F () {cout <"base2: F ()" <Endl;} virtual void g () {cout <"base2 :: G () "<Endl;} virtual void H () {cout <" base2: H () "<Endl;} protected: Private :}; // No overwrite. In this case, the virtual function will append the original virtual function table to the class derived: public base {public: Virtual void F1 () {cout <"derived: F1 ()" <Endl;} virtual void G1 () {cout <"derived: G1 ()" <Endl ;} virtual void H1 () {cout <"derived: H1 ()" <Endl ;}; // if so, the same function overwrites class derived_cover: public base {public: Virtual void F () {cout <"derived_cover: F ()" <Endl;} virtual void G1 () {cout <"derived_cover :: g1 () "<Endl ;}virtual void H1 () {cout <" derived_cover: H1 () "<Endl ;}; // multiple inheritance, no overwrite class multiplederived: public base, public base2 {public: Virtual void F1 () {cout <"multiplederived: F1 ()" <Endl;} virtual void G1 () {cout <"multiplederived: G1 ()" <Endl ;}; // multi-inheritance, with overwrite class multiplederivedcover: public base, public base2 {public: virtual void F () {cout <"multiplederivedcover: F ()" <Endl;} virtual void G1 () {cout <"multiplederivedcover: G1 () "<Endl ;}}; typedef void (* Fun) (void); // test the storage relationship between the virtual function and the virtual function table void test1 () {Base B; fun pfun = NULL; cout <"virtual function table address" <(int *) * (int *) (& B) <Endl; cout <"first function address of the virtual function table" <(int *) * (int *) (& B) <Endl; cout <"virtual function table second function address" <(int *) * (int *) (& B) + 1) <Endl; cout <"virtual function table third function address" <(int *) * (int *) (& B) + 2) <Endl; // directly access the function content in the virtual function table through the pointer pfun = (fun) (int *) * (int *) (& B ); pfun (); pfun = (fun) (int *) * (int *) (& B) + 1); pfun (); pfun = (fun) (int *) * (int *) (& B) + 2); pfun ();} // if the test does not cover the function, add void Test2 () {Base B; derived derive; fun pfun = NULL to the end of the original virtual function table; cout <"virtual function table address" <(int *) * (int *) (& B) <Endl; cout <"virtual function table address" <(int *) * (int *) (& derive) <Endl; cout <"first function address of the virtual function table" <(int *) * (int *) (& B) <Endl; cout <"first function address of the virtual function table" <(int *) * (int *) (& derive) <Endl; cout <"virtual function table second function address" <(int *) * (int *) (& B) + 1) <Endl; cout <"virtual function table second function address" <(int *) * (int *) (& derive) + 1) <Endl; cout <"virtual function table third function address" <(int *) * (int *) (& B) + 2) <Endl; cout <"virtual function table third function address" <(int *) * (int *) (& derive) + 2) <Endl; // print out the virtual function content pfun = (fun) (int *) * (int *) (& derive); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 1); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 2); pfun (); pfun = (fun) (int *) * (int *) * (int *) (& derive) + 3); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 4); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 5 ); pfun ();} // overwrite the original function, and the void test3 () {Base B; derived_cover derive; fun pfun = NULL; cout <"virtual function table address" <(int *) * (int *) (& B) <Endl; cout <"virtual function table address" <(int *) * (int *) (& derive) <Endl; cout <"first function address of the virtual function table" <(int *) * (int *) (& B) <Endl; cout <"first function address of the virtual function table" <(int *) * (int *) (& derive) <Endl; cout <"virtual function table second function address" <(int *) * (int *) (& B) + 1) <Endl; cout <"virtual function table second function address" <(int *) * (int *) (& derive) + 1) <Endl; cout <"virtual function table third function address" <(int *) * (int *) (& B) + 2) <Endl; cout <"virtual function table third function address" <(int *) * (int *) (& derive) + 2) <Endl; // print out the virtual function content pfun = (fun) (int *) * (int *) (& derive ); // F overwrites F of the parent class pfun (); pfun = (fun) (int *) * (int *) (& derive) + 1); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 2); pfun (); pfun = (fun) (int *) * (int *) (& derive) + 3); // pfun (); pfun = (fun) (int *) * (int *) (& derive) + 4); pfun (); cout <"if the function implemented by the subclass is called directly through the parent class Pointer" <Endl; base * newbase = new derived_cover; newbase-> F (); newbase-> G (); newbase-> H ();} // test the Memory Distribution of the Multi-inherited virtual function table, there should be two virtual function tables, // pointing to base and base2 respectively, without overwriting void test4 () {cout <"Multi-inheritance Memory Distribution" <Endl; multiplederived mymultiple; fun pfun = NULL; cout <"Multi-inherited virtual function 1 Address" <(int *) * (int *) (& mymultiple) <Endl; cout <"Multi-inherited virtual function 2 address" <(int *) * (int *) (& mymultiple) + 1) <Endl; cout <"virtual function table 1 should be a function not covered by base function + subclass" <Endl; pfun = (fun) (int *) * (int *) * (int *) (& mymultiple); // fun1 base: F () pfun (); pfun = (fun) (int *) * (int *) * (int *) (& mymultiple) + 1); // The fun2 base of the ALI group: G () pfun (); pfun = (fun) (int *) * (int *) (& mymultiple) + 2); // fun3 base: H () pfun (); pfun = (fun) (int *) * (int *) (& mymultiple) + 3); // fun4 multiplederived: F1 () pfun (); pfun = (fun) (int *) * (int *) (& mymultiple) + 4); // fun4 multiplederived: G1 () pfun (); cout <"virtual function table 2" <Endl; pfun = (fun) (int *) * (int *) (& mymultiple) + 1); pfun (); pfun = (fun) (int *) * (int *) (& mymultiple) + 1) + 1); pfun () ;}// test that multiple inheritance types include void test5 () {cout <"multiple inheritance types include override" <Endl; multiplederivedcover D; base * D1 = & D; base2 * D2 = & D; D1-> F (); D2-> F (); D1-> G (); d2-> G () ;}int main () {test1 (); Test2 (); test3 (); test4 (); test5 (); Return 0 ;}

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.