Two questions related to virtual functions

前端 未结 4 1177
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  执念已碎
    2020-12-20 02:59

    for question 1: i think the paragraph is just saying that the vtables aren't actually merged into one and stored in the memory allocation of the class that is multiply deriving; but that the vtables of the base classes beyond the first base classes are used by reference.

    in other words; if you have Rose which derives from Flower which derives from Plant then Rose only incorporates Flower's vtable directly, but uses of Plant's vtable are done by calling them from Plant's vtable. )

    for question 2: i am not so great at doing these in my head, i would have to just start breaking it down into manageable chunks to understand.

    first i would tabify it like so:

    (
      *(foo)
      (
        (void**)
        (
          ((void**)(&a))[0]
        )
      )
      [1]
    )
    (); 
    

    then,

    Step 1:

    ((void**)(&a))[0]

    we know (X)[0] = *X

    let X = (void**)&a

    X[0] = ((void**)&a)[0] = (void*)a

    now replace:

    (
      *(foo)
      (
        (void**)
        (
          (void*)(a)
        )
      )
      [1]
    )
    (); 
    

    Step 2:

    (void**)((void*)(a)) = (void**)(void*)a = (void**)a

    (
      *(foo)
      (
        (void**)a
      )
      [1]
    )
    (); 
    

    Step 3:

    so it looks like we are left with a function pointer

    (foo)((void**)a)[1] = (foo)((void*)(a+1))

    or, what looks like a void* to function at position (a+1), of type foo...

    i think that's at least close to right :) function pointers always give me problems. ;)

提交回复
热议问题