RE: [GD-General] Overhead of RTTI
Brought to you by:
vexxed72
From: Nicolas R. <nic...@fr...> - 2002-12-24 13:55:16
|
Hi, Built-in RTTI is well suited for complex inheritance diagrams, but its overhead is not neglectable (just like C++ exception handling). I'm using exactly the same "kind" of hierarchy, banning multiple inheritance, and just adding a few things: The "Object" class is declared as: { public: static ObjectClass itsClass; virtual ObjectClass* GetClass(void) const { return &thisObjectClass; } inline const char* GetClassName(void) const { return GetClass()->Name(); } inline bool IsOfClass(const ObjectClass& oc) const { return (GetClass() == &oc); } }; Inherited classes (that do need RTTI) do also have: { public: Static ObjectClass itsClass; virtual ObjectClass* GetClass(void) const { return &thisObjectClass; } }; Whenever you want to check for an object being of a given class (something like your rendering device checking that the vertex buffer pointer is a D3D vertex buffer), you just have to call the GetClass and check returned value with the D3D::thisClass. (Or in the exemple above, using the IsOfClass method). Note that the above implementation is nice since it is still safe to use the RTTI methods in constructors/destructors. It turned out (for me) to be faster than built-in RTTI system, and a lot smaller (in terms of code/memory usage). Of course, my implementation is a bit more complicated, with ObjectClass constructors building the class hierarchy, making possible to check for inheritance, or more complex behaviors, maintaining a constructor list (for dynamic construction), and things like that... The "thisClass" object is also a nice place to put static tweakable values. The nice thing is that you can "forget" your own RTTI declaration where it is not needed (for example abstract class you don't need to know about), saving some memory/code-size (and eventually speed when checking for inheritance). Using macros like RTTI_DECLARE usually makes life easier in that case :) #define RTTI_DECLARE_EX(cloname) public: \ static cloname _class; \ virutal ObjectClass* GetClass(void) const { return &_class; } #define RTTI_DECLARE RTTI_DECLARE_EX(ObjectClass) Nicolas Romantzoff |