   sorting using RTTI?
  Is it reasonable to sort multiple objects with C++ RTTI? Or should I simply make a virtual function that will return a string with a class name and use that for sorting the objects?Also, if there's a better method of doing this, please enlighten me.Thank you in advance.   
  Hi,Quote:Is it reasonable to sort multiple objects with C++ RTTI? Or should I simply make a virtual function that will return a string with a class name and use that for sorting the objects?My question would be... What do you want to sort?It could depends on some factors... like some people claim that RTTI slows the execution of your program because the classes and pointers need to keep track of their type and when you use dynamic_cast<>() your not garanteed of the time it takes to return.  Others might claim that this overhead is the same or only a bit worse than what you could do by implementing your own type information classes (you might like to check "Game Programming Gems 2" for an example on how to implement it yourself).Personnally I would say stay as far as possible from solution that needs the type of a class to work. From what I heard the problem is not really that some compilers are not supporting it but that since the implementation is compiler specific your compiled libraries won't be usable with other compilers (or you would need to compile with their compilers for them to be able to use it).From my point of view, inheritance and polymorphism are there to hide most of the differences between they objects that are of the same type. Try to find another way to sort your objects... if there is one.For example, if you want to sort your scene graph to minimize state changes you might keep another structure next to it that will hold your objects in a tree-like structure (look for shader graph) that will automatically add the objects where they belong and sort them that way.Another example, you want to sort objects in a particular order, objects of type A first, then objects of type B, etc. Maybe a structure like a priority queue (a kind of heap) will help you more and all the sort operations would be based in the "base" structure (here the priority queue). The priorities could be stored in each objects through some common interface like "virtual int GetPriority() const" and use that to sort the objects.Anyway, those are just examples I just "made up" to explain things :P.  Maybe RTTI is the solution for your problem but like I said, stay away from solutions specific to the actual type of objects (like RTTI and DTI (Dynamic Type Information in Game Programming Gems 2)).  But if you need runtime type info, consider the DTI approach so every object have their type information implemented in their class. This makes things a little more object oriented than using some compiler specific solution.JFF   ;Quote:Original post by AtashIs it reasonable to sort multiple objects with C++ RTTI? Yes. You can sort objects based on the result of typeid. typeinfo objects are ordered. Looking up the type of an object is actually cheap (it's walking through hierarchies that isn't).#include <typeinfo>#include <algorithm>#include <functional>template<class T>struct order_by_type :   public std::binary_function<T,T,bool>{   bool order_by_type(const T& a, const T& b) const   { return typeid(a) < typeid(b);   }};int main(){   vector<Foo*> vec;   // Fill it somehow.   std::sort(vec.begin(), vec.end(), order_by_type<Foo*>());}