In C++, "dynamic_cast" being slow is a known fact.
It was a known fact back thirty years ago (when I first started using C++ compilers) 1989/1990 (back then I used whatever compiler was installed by default on the sun workstations). BUT that is no longer true for modern compilers.
I remember downloading g++ (2.4 I think; approx 1996-1998) to look at what was happening (see below). The compiler (g++) would traverse the class hierarchy and at each level do a class name string comparison as it searched for the correct type record. This was done as each compilation unit potentially had its own unique set of type record (so you could not simply compare pointers).
And if not, then why isn't it a common practice
As a result a lot of people actually did write their own versions of dynamic cast to improve speed (but then again people were still trying to work out how to write good C++ in those days and most code was written like C so performance was probably not the fault of dynamic_cast). But the couple of implementations I saw were exceedingly brittle and subject to a lot of bugs. Which is why I was looked at how g++ worked. By this time (1998) the self written dynamic cast my company used was showing its age and I ripped it out to use the compiler version (no measurable performance change, but lots of mysterious bugs disappeared).
Thankfully this was fixed a long time ago. Before the end of the last century (I can not give you an exact date). But definitely prior to g++ 3.0.
I thought of following simple way of knowing the type of an object in a hierarchy.
You have identified the class by a number. This is not the same as a cast. You still need to convert the pointer to the correct location.
Could someone please explain if this could be slower than dynamic_cast?
I would suspect that you would have a hard time writing a version faster than the current implementation. Especially taking into account all the special corner cases with virtual inheritance and such. To be honest I doubt you could do a faster one for even a simple hierarchy like you show. The compiler has 30 years of optimization tricks applied to it. These tricks are applied at compile time your result is run time.
given that speed is the worst drawback of C++ over C?
I find this hard to believe. Do you have a citation?