Re: compare two objects, if they have the same vtable
Bart van Ingen Schenau schrieb:
Albert Zeyer wrote:
Mathias Gaunard schrieb:
On 1 oct, 20:38, Albert Zeyer <albert.z...@rwth-aachen.de> wrote:
I thought RTTI adds runtime type information to each class and
typeid is based on these additional information.
RTTI adds information in the vtable. It not only contains function
pointers, but also a name and information to tell whether the object
derives from some classes efficiently or not (which is required for
dynamic_cast).
I don't need the overhead of runtime type information just to
compare the vtable which is there anyway.
There is no overhead with typical implementations.
Since there is more data, however, it takes a bit more space in your
data segment.
Wouldn't it add the vtable pointer to each class/struct, also to
those
which had no virtual functions before?
No, because that would break compatibility with C.
RTTI and dynamic_cast<> are defined in such a way that a runtime
lookup
is only needed if the object can have polymorphic behaviour, as
indicated by the presence of a virtual member function.
Ah, that's interesting. I always thought it adds the vtable-pointer to
everything then.
If you try to use RTTI with a non-polymorphic object, all the
information will be (assumed to be) present at compile time, so there
is no need to add an otherwise useless vtable/vptr.
So, the following is not possible then?
class Base { public: int data1; };
class Derived : public Base { public: int data2; };
Base* obj = new Derived();
Derived* d = dynamic_cast<Derived*>(obj);
And note that the C++ standard does not provide for the possibility to
turn off RTTI. As POD structs are meant to be compatible with how they
are defined in C and RTTI is non-optional, there is no way that a
conforming compiler can add something like a vptr to a POD struct.
Oh, I didn't knew that either.
Bart v Ingen Schenau
Thanks for the hints (also to the other replieers).
Albert
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]