C ++ Object Memory Layout-③ test the virtual function table of the derived class in Multi-Inheritance

Source: Internet
Author: User

C ++ Object Memory layout -- ③ test the virtual function table of the derived class in Multi-Inheritance

Test 2: the virtual function address of the derived class and the virtual function address of the first base class are saved in the same virtual function table.

The number of base classes that have virtual functions in the derived class, and the number of pointers to the virtual function table in the derived class object.

// Test the table of virtual functions in the Multi-inheritance derived class. CPP // 2010.8.18 // The test proves that the virtual function address of the derived class is saved in the same virtual function table as the virtual function address of the first base class. The number of base classes that have virtual functions in the derived class, and the number of pointers to the virtual function table in the derived class object. // Vs compiler # include <iostream> # include <string> using namespace STD; //////////////////////////////////////// /// // class base1 {public: base1 (INT a1 = 10): A1 (A1) {cout <"base1: base1 ()" <Endl;} virtual void show1 () {cout <"base1: show1 ()" <Endl;} PRIVATE: int A1 ;}; //////////////////////////////////////// /// // class base2 {public: base2 (INT a2 = 20): A2 (2) {cout <"base2: base2 ()" <Endl;} virtual void show2 () {cout <"base2: show2 () "<Endl;} PRIVATE: int A2 ;}; //////////////////////////////////////// /// // class derived: public base1, public base2 {public: derived (INT a3 = 100): A3 (A3) {cout <"derived: derived ()" <Endl ;} virtual void show3 () {cout <"derived: show3 ()" <Endl;} PRIVATE: int A3 ;}; ////////////// //////////////////////////////////////// //// // Int main () {derived aobj; int ** P = (INT **) & aobj; typedef void (_ thiscall * Fun) (void * pthis ); // very important cout <"derived class object size =" <sizeof (aobj) <Endl; cout <"derived class Object Memory layout:" <Endl; /* According to the output, we can find that the member variables are 0x0a, 0x14, and 0x64, respectively, the other two are virtual function table pointers */For (INT I = 0; I! = Sizeof (aobj)/4; ++ I) {cout <"0x" <p [I] <Endl ;} /* the output proves that the virtual function address of the derived class is stored together with the virtual function address of the first base class */cout <"virtual function table pointer-> virtual function table-> Function call: "<Endl; (fun) (p [0] [0]) (p); // base1: show1 () (fun) (P [0] [1]) (p); // derived: show3 () (fun) (p [2] [0]) (p + 2); // base2: show2 () system ("pause"); Return 0;}/* base1: base1 () base2: base2 () derived: derived () object size of the derived class = 20 memory layout of the derived class object: 0x0041c2540x0000000a0x0041c24c0x000000140x00000064 virtual function table pointer-> virtual function table-> function call: base1: show1 () derived :: show3 () base2: show2 () press any key to continue... */

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.