question: cost of dynamic cast?
No offense, but that's hideous. There's no reason to use strings in place of an enum or bit flags. You didn't provide your IsA implementation but if you're doing string compares and stuff it's not surprising that it would be slower than dynamic_cast. One of the big points of an alternative to dynamic_cast is to avoid the overhead. In your case you not only have created something slower, but you even managed to complicate the process significantly. Whatever floats your boat though.
The string version is not meant for speed (FYI it just uses a binary search to look up a TypeInfo, then passes it onto the regular IsA). Using strings as the lookup type lets you do things like letting the developers (yourself included) perform actions on certain types of objects in your game's hierarchy. You can type the class name into a dropdown console as an argument to a command, like "hide projectile" if you wanted to hide all 'Projectile' objects in your game level, or "count ammo" which might tell you how many 'Ammo' objects are in the level.
I already conceded that RTTI is faster regardless. I'm going after functionality.
I already conceded that RTTI is faster regardless. I'm going after functionality.
Quote:Original post by ToohrVykQuote:DELETEDOriginal post by moeron
It may be cheesy but I've always leaned more toward using ENUMS or some other method for determining RTTI.
I don't know if this is optimal for all solutions, but its worked for me. I'm sure there are many other similar ways to avoid dynamic casts.
This method is faster than dynamic_cast (could be as much as 5 times faster), and you can see an implementation of it in the above link. This has several problems:
- It's unsafe, you can create very nasty bugs by casting manually objects.
- It does not emulate dynamic_cast, because it cannot be used to cast to a supertype of the actual type, only to the actual type.
If you only need the reduced functionality of being able to cast to the actual type of the object, you can still use RTTI and typeof, as such:
if( typeof(object) == typeof(expected_type) ) { cast( ); } else { fail( ); }
This will not require the additional overhead of having to keep type information for your objects on your own.
Yea, after I posted that I read that article, thats why I deleted my original post =) You learn something new everyday though I suppose.
moe.ron
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement