Two questions related to virtual functions

前端 未结 4 1185
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-20 02:28

I was reading this articles , was at this heading Inheritance of Base-class vPtrs , but couldn\'t understand what did he mean in this para :

"However, due to mul

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-20 02:52

    If Derived inherits from Base, its vtable will extend Base's. Now, if it also inherits Base2, its vtable will not include Base2's - Base2's part will retain its vtable (updated with Derived virtual functions, if they override Base2's).

      Base members      Base2 members    Derived members
    +--+------------+----+------------+------------------+
     |                |
     V                V
    Derived + Base   Base2 vtable  
    vtable
    

    To make the second question easier to understand, and since I enjoy drawing with fixed width font... here's the memory layout of a. For a full explanation about that expression, see James Kanze's answer.

            +---+----------+
    a:      | | |    A     |
            +-+-+----------+
              |
              V
    vtable: +---+
            | --+--> f2()
            +---+
            | --+--> f3()
            +---+
    

    HTH...

提交回复
热议问题