user997112
user997112

Reputation: 30615

C++ VTable implementation with multi-parent polymorphic hierarchies

If I have

A <- B (where both are polymorphic)

there are two levels of indirection when calling a function on B. Firstly a look up to determine what type of A object and then a lookup to find the function amongst all of B's.

Now if I had:

A <- B <-C (and all are polymorphic classes)

does this mean when I call a function on C it has to go through four levels of indirection (two for B and two for A)?

Upvotes: 1

Views: 128

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

There is always just a look-up in the vtable and a call of the resulting function. The depth of the inheritance tree doesn't make a difference. When multiple inheritance is involved (i.e., a class with multiple bases) it may be necessary to also adjust the pointer which becomes the this pointer.

Upvotes: 3

Related Questions