On Apr 24, 12:16 pm, cppquester <cppques...@googlemail.com> wrote:
When I replaced the dynamic cast in the code excerpt below
(here each class knows it type), I gained a speedup of about factor
4(!)
(in release mode (-O2))
Why is a dynamic cast so expensive? I thought basically the RTTI
system basically does
what I did (store the type (implicitely) and then cast or return
NULL).
Or might this be specific to my platform (g++ 4.2.4)?
Thanks,
Marc
thisType* lEnd=dynamic_cast<thisType*>(derived);
if( lEnd == NULL)
throw MyException("Type changed.");*/
if( derived->Type() != this->type)
throw MyException("Type changed.");
thisType* lEnd=static_cast<thisType*>(derived);
Not enough info.
A class always knows its type, you don't need RTTI for that. In the
case where the object 'derived' above is of a type which is a
derivative of thisType, then derived will always be of type thisType.
So doing a dynamic cast is pointless, doing any cast is pointless.
So to answer your question: the question is irrelevant.
Btw, C++ programmers are reknown to be pathetic when it comes to
guessing. Try making a simple, short compileable example to explain
what you seek.
class thisType { };
class Derived : public thisType { };
int main()
{
// do stuff
}
You are right, it was not clear.