Re: Get address of whole instance with multiple inheritance
On Dec 12, 5:08 pm, Seungbeom Kim <musip...@bawi.org> wrote:
Bart van Ingen Schenau wrote:
dynamic_cast<> works only for polymorphic types (i.e. types with at
least one virtual function) and there is a special rule for casting
*to* a void*, which states you get the address of the most-derived
object.
Note that once you have a void*, you can only use a static_cast to get
back a usable pointer, and then you must be able to specify the actual
type of the object pointed to, which is Impl here.
If you're going to use static_cast<Impl*> after static_cast<void*>
anyway, why don't you just use dynamic_cast<Impl*> in the first place?
Is there any reason why static_cast<void*> might be preferred to
dynamic_cast<Impl*>?
I'm assuming you mean dynamic_cast<void*> here rather than
static_cast<void*> because I can't make any sense of it otherwise.
dynamic_cast<void*> seems designed for the case where you have
pointers to two different interfaces and need to know if they are the
same object; e.g. you are using the pointer as a key in a map. You may
not know what Impl is (the items in the map might not even be in the
same hierarchy) but you need a pointer to the most derived object to
ensure different interfaces to the same object map together.
Yechezkel Mett
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]