Please observe the below code. As far as i know, dynamic_cast is slower than static_cast. Because it evaluates the type at runtime.
My doubt here is if we use static_cast with typeid() as below , will it takes same time as dynamic cast ??
Will it be faster than dynamic_cast ?
class Shape
{
public:
virtual ~Shape(){}
};
class Circle : public Shape{ };
class Square : public Shape{ };
Static cast with RTTI:
Circle c;
Shape* s = &c; // Upcast: normal and OK
// More explicit but unnecessary:
s = static_cast<Shape*>(&c);
// (Since upcasting is such a safe and common
// operation, the cast becomes cluttering)
Circle* cp = 0;
Square* sp = 0;
// Static Navigation of class hierarchies
// requires extra type information:
if(typeid(s) == typeid(cp)) // C++ RTTI
cp = static_cast<Circle*>(s);
if(typeid(s) == typeid(sp))
sp = static_cast<Square*>(s);
if(cp != 0)
cout << "It's a circle!" << endl;
if(sp != 0)
cout << "It's a square!" << endl;
Dynamic cast:
Circle c;
Shape* s = &c; // Upcast: normal and OK
s = &c;
Circle* cp = 0;
Square* sp = 0;
cp = dynamic_cast<Circle*>(s);
if(cp != 0)
cout << "It's a circle!" << endl;
sp = dynamic_cast<Square*>(s);
if(sp != 0)
cout << "It's a square!" << endl;
It is faster to test the type and then do the
static_cast, but the operations are not equivalent as that will only allow downcast to the most derived type (any intermediate level will not be matched with thetypeid). I would usedynamic_castas it is more robust (will not break if someone extends your type and passes a pointer, for example).If performance of
dynamic_castis an issue in your application, you should reconsider the design. Whiletypeid+static_castis faster thandynamic_cast, not having to switch on the runtime type of the object is faster than any of them.