Re: C++ rtti hack
On May 12, 8:59 am, brang...@ntlworld.com (Dave Harris) wrote:
bji-gg...@ischo.com () wrote (abridged):
I would test it with a class like:
struct Base {
int baseData;
};
struct Derived: Base {
int moreData;
virtual ~TestThis() {}
int evenMoreData;
}
Derived testThis;
I would expect the compiler to store the RTTI in the vtable, and the
classes could be laid out like:
struct _Rtti {
struct __VTable *__pVTable;
};
struct _Base {
int baseData;
};
struct _Derived {
int baseData;
int moreData;
struct __VTable *__pVTable;
int evenMoreData;
};
If you are lucky the compiler might move pVTable to in front of moreData,
but it's unlikely to move it in front of baseData. So the offset of the
vtable is different in Derived to Rtti and LookupTypeInfo( &testThis )
will likely fail, when it ought to succeed.
You are absolutely right, your example does fail on g++. However, it
succeeds if Base has a vtable too (i.e. put a ~Base() { } in there).
Here is something interesting:
------------------------------------------------------------
#include <stdio.h>
#include <typeinfo>
struct Base
{
int baseData;
};
struct Derived : Base
{
int moreData;
virtual ~Derived() { }
int evenMoreData;
};
class Rtti
{
virtual ~Rtti() { }
};
const std::type_info &LookupTypeInfo(void *pObjectWithVtable)
{
return typeid(* (Rtti *) pObjectWithVtable);
}
int main()
{
Derived d;
const std::type_info &typeInfoD = typeid(d);
const std::type_info &typeInfoBase = typeid((Base &) d);
const std::type_info &typeInfoLookedUpD = LookupTypeInfo(&d);
const std::type_info &typeInfoLookedUpBase = LookupTypeInfo((Base
*) &d);
printf("d name: %s\n", typeInfoD.name());
printf("base name: %s\n", typeInfoBase.name());
printf("looked-up d name: %s\n", typeInfoLookedUpD.name());
printf("looked-up base name: %s\n", typeInfoLookedUpBase.name());
return 0;
}
------------------------------------------------------------
This program outputs:
d name: 7Derived
base name: 4Base
looked-up d name: 7Derived
Segmentation fault (core dumped)
So the final lookup is not working. I thought about it for a minute
an then I realized why: the call:
LookupTypeInfo((Base *) &d);
violates the "rules" of using LookupTypeInfo: it is not being called
on an object with a vtable. Since my requirements for using
LookupTypeInfo are that it is called on objects with vtables, the
counterexample doesn't disprove that what I am doing works. You just
always have to call on an object with a vtable. I personally never
write objects that "add" vtables when inheriting from non-virtual
classes. It is just asking for trouble. For example, this fails
miserably:
------------------------------------------------------------
#include <stdio.h>
struct Base
{
int baseData;
};
struct Derived : Base
{
int moreData;
virtual ~Derived() { }
int evenMoreData;
};
int main()
{
Derived *d = new Derived();
Base *b = (Base *) d;
delete b;
return 0;
}
------------------------------------------------------------
But if a vtable is added to Base, then it works just fine.
I still contend that if the requirements of my LookupTypeInfo method
are met (i.e. a pointer to an object with a vtable are passed in),
then it will *always* work on *every* compiler - because there is no
way for the compiler to put the rtti information in a place that it
cannot find regardless of the actual object type being pointed at.
Same for virtual destruction. I'll bet that this weird thing will
also work on all C++ compilers:
------------------------------------------------------------
#include <stdio.h>
struct Base
{
int baseData;
virtual ~Base() { printf("Base deleted\n"); }
};
struct Derived : Base
{
int moreData;
virtual ~Derived() { printf("Derived deleted\n"); }
int evenMoreData;
};
class Delete
{
public:
virtual ~Delete() { }
};
void DeleteAnyClassWithAVtable(void *pObjectWithVtable)
{
delete (Delete *) pObjectWithVtable;
}
int main()
{
Derived *d = new Derived();
DeleteAnyClassWithAVtable(d);
return 0;
}
------------------------------------------------------------
I checked it; it works fine on g++.
Thanks!
Bryan
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]