Multiple Inheritance Memory layout analysis

Source: Internet
Author: User

VC + + 2005/GD//acronym (Root level) = RL//acronym (Second level) = SL//acronym (Third level) = TL #include <stdio .h> typedef void (*VFUNCPTR) (); typedef vfuncptr* VTable; Class RL1 {public:virtual void Vf_rl1 () {} public:int _i_rl1;}; Class RL2 {public:int _i_rl2;}; Class RL3 {public:virtual void Vf_rl3 () {} public:int _i_rl3;}; Class Sl1:public RL1, public RL2, public RL3 {public:int _I_SL1; Class Sl2:public RL1, public RL2, public RL3 {public:virtual void Vf_sl2 () {} public:int _I_SL2; Class Sl3:public RL2, public RL3 {public:int _i_sl3;}; Class Sl4:public RL2, public RL3 {public:virtual void Vf_sl4 () {} public:int _i_sl4;}; To analyze this, SL1 and SL2 are better off using virtual inheritance. Do it again when you are free. Class TL://Public SL1, public SL2//{//public://virtual void Vf_tl () {}//public://int _i_tl;//}; int main () {RL1 rl1; rl1._i_rl1 = 0x11; int RL1::* pdm_rl1 = NULL; pdm_rl1 = &RL1::_i_rl1; printf ("0x%p:0x%x/n", pdm_ RL1, RL1.*PDM_RL1); void (RL1::* pmf_rl1) () = &rl1::vf_RL1; QuickWatch identifier PMF_RL1, associated with thunk. printf ("&AMP;RL1::VF_RL1:%p/n", PMF_RL1); The printed value is not the same as the value in the virtual function table, WHY? VTable vt_rl1 = * (vtable*) &rl1; In the Watch window, see the contents of the VT_RL1 subscript from 50//to 200. Note: Pay attention to vt_rl1[-1], and RTTI related. printf ("--------------------------------/n"); RL2 Rl2; Rl2._i_rl2 = 0x22; VTable vt_rl2 = * (vtable*) &rl2; VT_RL2; printf ("--------------------------------/n"); RL3 Rl3; Rl3._i_rl3 = 0x33; VTable vt_rl3 = * (vtable*) &rl3; VT_RL3; printf ("--------------------------------/n"); SL1 SL1; SL1._I_RL1 = 0x11; Sl1._i_rl2 = 0x22; Sl1._i_rl3 = 0x33; SL1._I_SL1 = 0x1111; int SL1::* PDM_SL1 = NULL; PDM_SL1 = &SL1::_i_rl1; printf ("0x%p:0x%x/n", PDM_SL1, SL1.*PDM_SL1); PDM_SL1 = &SL1::_i_rl2; printf ("0x%p:0x%x/n", PDM_SL1, SL1.*PDM_SL1); PDM_SL1 = &SL1::_i_rl3; printf ("0x%p:0x%x/n", PDM_SL1, SL1.*PDM_SL1); PDM_SL1 = &SL1::_i_sl1; printf ("0x%p:0x%x/n", PDM_SL1, SL1.*PDM_SL1); Look at the Watch window: vt_sl1_1, 2 VTable vt_sl1_1 = * (vtable*) &sl1; InWatch window look: vt_sl1_2, 2 VTable vt_sl1_2 = * (vtable*) ((char*) &AMP;SL1 + sizeof (void*) + sizeof (int)); printf ("--------------------------------/n"); SL2 SL2; SL2._I_RL1 = 0x11; Sl2._i_rl2 = 0x22; Sl2._i_rl3 = 0x33; SL2._I_SL2 = 0x2222; int SL2::* PDM_SL2 = NULL; PDM_SL2 = &SL2::_i_rl1; printf ("0x%p:0x%x/n", PDM_SL2, SL2.*PDM_SL2); PDM_SL2 = &SL2::_i_rl2; printf ("0x%p:0x%x/n", PDM_SL2, SL2.*PDM_SL2); PDM_SL2 = &SL2::_i_rl3; printf ("0x%p:0x%x/n", PDM_SL2, SL2.*PDM_SL2); PDM_SL2 = &SL2::_i_sl2; printf ("0x%p:0x%x/n", PDM_SL2, SL2.*PDM_SL2); Look at the Watch window: vt_sl2_1, 3 VTable vt_sl2_1 = * (vtable*) &sl2; Look at the Watch window: vt_sl2_2, 2 VTable vt_sl2_2 = * (vtable*) ((char*) &AMP;SL2 + sizeof (void*) + sizeof (int)); printf ("--------------------------------/n"); SL3 SL3; Sl3._i_rl2 = 0x22; Sl3._i_rl3 = 0x33; SL3._I_SL3 = 0x3333; int SL3::* pdm_sl3 = NULL; PDM_SL3 = &SL3::_i_rl2; printf ("0x%p:0x%x/n", PDM_SL3, SL3.*PDM_SL3); PDM_SL3 = &SL3::_i_rl3; printf ("0x%p:0x%x/n", pdm_SL3, SL3.*PDM_SL3); PDM_SL3 = &SL3::_i_sl3; printf ("0x%p:0x%x/n", PDM_SL3, SL3.*PDM_SL3); Look at the Watch window: VT_SL3, 2 VTable vt_sl3 = * (vtable*) &sl3; printf ("--------------------------------/n"); SL4 SL4; Sl4._i_rl2 = 0x22; Sl4._i_rl3 = 0x33; SL4._I_SL4 = 0x4444; int SL4::* pdm_sl4 = NULL; PDM_SL4 = &SL4::_i_rl2; printf ("0x%p:0x%x/n", PDM_SL4, SL4.*PDM_SL4); PDM_SL4 = &SL4::_i_rl3; printf ("0x%p:0x%x/n", PDM_SL4, SL4.*PDM_SL4); PDM_SL4 = &SL4::_i_sl4; printf ("0x%p:0x%x/n", PDM_SL4, SL4.*PDM_SL4); Look at the Watch window: VT_SL4, 3 VTable vt_sl4 = * (vtable*) &sl4; printf ("--------------------------------/n"); return 0; }

0x00000004:0x11 &rl1::vf_rl1:0041110e--------------------------------0x00000004:0x11 0x00000010:0x22 0x0000000c:0x33 0x00000014:0x1111--------------------------------0x00000004:0x11 0x00000010:0x22 0x0000000c:0x33 0 x00000014:0x2222--------------------------------0x00000008:0x22 0x00000004:0x33 0x0000000c:0x3333--------------- -----------------0x00000008:0x22 0x00000004:0x33 0x0000000c:0x4444--------------------------------

Analysis results:
1. SL1 Object


2. SL2 Object


3. SL3 Object


4. SL4 Object

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.