3 ms·
Classes and inheritance shouldn’t really have an impact on pointers though.
by kortilla 5y ago
Classes and inheritance shouldn’t really have an impact on pointers though.
- jussij 5y agoI suspect the OP is referring to the fact C++ uses vtables which means function calls and object layout make heavy use of pointers.
- tgsovlerkhgsel 5y agoThank you! It was a while since I last touched the project, this is exactly what I meant. It gets really confusing if you aren't aware of the concept.
- st_goliath 5y agoIn 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!).
- steerablesafe 5y agoI think they meant virtual function calls, that manifests in indirect calls.