IrrelevantGaymer
IrrelevantGaymer

Reputation: 31

How does polymorphism with multiple inheritance work in assembly?

so I believe I understand polymorphism with single inheritance, where say you have classes Dog and Cat inherit from an interface Animal and Animal has a Speak and Walk function so then you generate v tables for Dog and Cat, both having a function pointer to their respective Speak and Walk functions, and if I’m calling a general one of those functions for a general Animal, assembly will grab the first data member of the Animal which is a pointer to the v table, dereference it, then add the correct offset to get the correct function pointer, then call said function at the memory address.

So my question is how would this work for multiple inheritance. So for the example, I will do Dog and Cat inheriting from interfaces Animal and Colorable: Animal will have functions Speak and Walk, and Colorable will have GetColor and SetColor. Would Dog have have a v table to Animal functions and then a v table to Colorable functions? Would that mean that every Colorable class would have memory padding so that way its v table is the second member so that you can do polymorphism with Colorable? Or would you have a triple pointer pointing to a table of v table pointers so that you only have class instances of the memory padding as opposed to object instances? Wouldn’t dereferencing a triple pointer be slow?

Is there another memory schema that I’m not considering? I haven’t found many good resources on this.

I know C#, Java, Rust, etc. supports polymorphism with multiple inheritance, so I feel like this is a solved thing.

I have tried searching on YouTube and stack overflow and Google, none of which have given me good results. I did find a page regarding Java doing multiple inheritance but to me it was kind of cryptic and hard to understand.

Edit: To add clarification, I’m looking specifically for single layered inheritance, like Rust’s trait syntax. Regarding C# and Java multiple inheritance, I am referring to the ability to use multiple interfaces for an object and be able to use said interface as a type for static polymorphism. Rust allows for implementing multiple traits.

Upvotes: 3

Views: 195

Answers (1)

user22405329
user22405329

Reputation: 979

None of the languages you say implement inheritance in the way you describe. You are describing C++ - it's in C++ you have a pointer to vtable at the beginning of a class.

In C++, the first base class gets merged with the derived: the vtable pointer in the beginning of the Dog is also a vtable pointer for the inscribed Animal base class (if Animal is first). The vtable would start with Animal virtual functions (Speak, Walk), then other base classes functions (GetColor, SetColor), then other virtual finctions of Dog, if any.

All other base classes will have a different vtable. That means the inscribed Colorable vtable pointer will point to a different vtable of Dog - the one that has GetColor and SetColor first. Moreover, those virtual functions will not be the same as the ones in the main vtable! They would be a special versions, that correct the pointer first (to point on Dog instance, instead of the inscribed Colorable instance).

In Java, there is a vtable for each base interface, and the runtime does a search in the list of interfaces to find a correct one. It is pretty much the same in C#, with minor differences. The JIT-compiler can optimize that, if needed.

In Rust, there are no pointers to vtable inside the "classes" at all. Instead, Rust uses "fat pointers" for the interfaces themselves - references to the interface are double sized, because they contain two pointers: one for the object, one for the vtable. The pointer to the vtable is only produced when you convert an object to a &dyn Interface type - not before. The pointer points to the correct position inside the vtable, so the current interface functions will appear first:

  • &dyn Animal will be equal {&Dog, &Dog_vtable + 0},
  • &dyn Colorable will be equal {&Dog, &Dog_vtable + 16}, etc

Upvotes: 4

Related Questions