Re: [GD-General] Multiple Inheritance and RTTI
Brought to you by:
vexxed72
From: Peter D. <pd...@mm...> - 2003-02-05 11:59:03
|
Alex Lindsay wrote: > Has anyone considered comparing the vtable pointers themselves in > order to identify a class? > > As far as I understand it, every polymorphic class is going to have a > unique vtable pointer, and while it's compiler dependent, my > experience is that the vtable pointer tends to be the first "member" > data in every instance. > > i.e. int vtable = *reinterpret_cast<int*>(someInstancePointer); > > I'm not too sure about multiple inheritance. It depends on your compiler. The MSVC object model always places the vptr at offset 0. struct X { int x; }; struct Y: public X { virtual ~Y(); }; On MSVC, Y::__vptr is at offset 0, and the X subobject is at offset 4. Note that this means that if you put Y* into a void* and then get X* from that void*, you'll be surprised. And there's no MI in sight. Other compilers, on the other hand, prefer to leave X at offset 0, and Y::__vptr goes at offset 4. Another interesting tidbit is that the vtable is not guaranteed to be unique. Even on MSVC, where the linker is very good at merging equal things, you could have one vtbl in the EXE and one in the DLL. The Windows loader isn't that good yet. This applies to &typeid(x), too. |