Virtual tables and virtual pointers for multiple virtual inheritance and type casting

后端 未结 3 1514
你的背包
你的背包 2021-02-04 13:48

I am little confused about vptr and representation of objects in the memory, and hope you can help me understand the matter better.

  1. Consider B inhe

3条回答
  •  执念已碎
    2021-02-04 14:12

    1. If object B inherits from A then the memory representation for B will be the following:

      • pointer to the virtual table of A
      • A specific variables/functions
      • pointer to the virtual table of B
      • B specific variables/functions/overrides

      If you have B* b = new B(); (A)b->f() then:

      • if f was declared as a virtual function then the B's implementation is called because b is of type B
      • if f was not declared as a virtual function then when called there will be no lookup in he vtable for the correct implementation and A's implementation will be called.
    2. Every object will have it's own vtable (don't take this for granted, as I have to research it

    3. Take a look at this for an example of vtable layour when dealing with multiple inheritance

    4. See this for a discussion about the diamond inheritance and the vtable representation

提交回复
热议问题