4 ms·
In C++ you have an implicit this pointer passed to methods, so for a start, something like this: instance.Method(foo); .... SomeClass::Method(int x
by st_goliath 5y ago
In C++ you have an implicit this pointer passed to methods, so for a start, something like this:
instance.Method(foo);
....
SomeClass::Method(int x) {
y += x;
}
more or less translates to something like this (using a pseudo C level here, rather than assembly):
Method(&instance, foo);
....
Method(SomeClass *this, int x) {
this->y += x;
}
it gets more interesting if "SomeClass::Method" is virtual, in which case the call looks more like this:
(((BaseClass *)&instance)->vtable.Method)(&instance, foo);
Where "vtable" is a compiler generated struct with function pointers. So here's your "pointery mess", neatly abstracted away by the syntax in the first example.
With multiple inheritance, it gets a bit more hairy than this, because the compiler has to do some offset adjustments on the casted pointer to get at the right vtable.
- quietbritishjim 5y agoAlthough rare, virtual inheritance (used in multiple inheritance) makes things even worse because even "this" needs to be found through a vtable! So you get another level of indirection. I was going to illustrate with an example like yours but I couldn't figure my way out through all the bits (including the virtual-base offsets in the vtable, or the VTT which is a table of vtables!).