Quote:Original post by Nypyren
(you can make a dummy instance - I wish C++ had a construct to get direct access to the vtable pointer without a nasty hack like this)
There is no such thing as a vtable pointer in C++, citizen.
The output of certain compilers may include a vtable pointer as an implementation of dynamic binding of methods. Other compilers, or certain virtual machines, may also implement dynamic binding of methods through a hash system or by decorating objects with their methods directly. While you can expect the usual C++ compilers to use a virtual table, you cannot assume that this is the case, lest you explicitly violate the C++ Standard and end up with undefined behaviour.
Also note that your hack can only be expected to work if the object is of the expected type. In particular, if C is-a B is-a A, then a reference to A storing a C will not be converted to a B this way. dynamic_cast and a virtual method would work here, however.
As for the work done by dynamic_cast: it's mostly a type lattice traversal (although it's not a lattice, but let's assume it is) which checks that the required type is a supertype of the dynamic type. This traversal is linear (straight up) when not using multiple inheritance, but it may need to look for several paths when multiple inheritance is used.