On Friday, 10 April 2015 07:35:00 UTC+3, Norman J. Goldstein wrote:
> Aside from writing virtual methods to do the job, I am wondering whether
> there is something like type_index/type_info to divulge the (void*)
> address of the most derived type.
You must have pointer to non-virtual base class to find out most
derived type with 'typeid(*ptr)'; 'void' is not base class. If you
know exactly from pointer to what type you did cast implicitly or
with 'static_cast' to 'void*' then you can 'static_cast' it back.
If you 'dynamic_cast<void*>' then you get address of most
non-virtually derived object, that is actually not what you want
unless you exactly know the type.
> On a related question, when
> restricting to single inheritance, are compilers obliged to have the
> same (void*) address for each class in the hierarchy?
On case of single *and* non-virtual inheritance such classes in practice
usually have same address but it is not guaranteed. The practice is
because of required order of initialization and required implicit casts
and finding vtable location (existence of what is not guaranteed at all)
are likely cheaper to implement. On case of virtual inheritance the
address is not same usually. Virtual inheritance is most expensive and
tricky. Introducing it into language was likely mistake, but I don't
care since I never use it. On case of multiple inheritance the different
base subobjects can not be at same address by simple logic.
Generally it is better to avoid casting. If you really need to then
consider making some special functions or member functions that do
it. It is easier to instrument such function with debug checks and
breakpoints.