   [C++] Can I find out what type a class is of?
  Hi all!Is there any way to something like this?(Pseudo code)Object* GetMainObject(){   Foreach entry   {  if(entry class is "main" or a child of)  { return entry;  }   }}This would be very nice :)   
  One solution could be to use RTTI or hand-roll your own solution. Of course, you may find (depending on what you're doing) that something like the Visitor Pattern may be useful - for example if you're trying to do something like if (this is type a) then else if (this is type b) then...   ;  RTTI seems to be fine. Does it works with children of that class, too?I don't need to do that if(this class) else if(that class) thing. There are some objects stored in a package, and only one of these objects can be placed in the world. But there are others, like components, helpers, etc. And the order is not always the same. And I need to get the only placeable object.Here, I found this at the page you linked:Quote:TIP: Certain compilers, such as Visual C++, disable RTTI by default to eliminate performance overhead. If your program does use RTTI, remember to enable RTTI before compilation. How slow is it? I don't need it for all my code, just for that one function...EDIT: Reading further I found the dynamic_cast. Does it solve it?   ;Quote:Original post by mind in a boxReading further I found the dynamic_cast. Does it solve it?Yes.Main* GetMainObject(){   Foreach entry   {  if (Main* main = dynamic_cast<Main*>(entry))  { return main;  }   }};Quote:Original post by DevFredQuote:Original post by mind in a boxReading further I found the dynamic_cast. Does it solve it?Yes.Main* GetMainObject(){   Foreach entry   {  if (Main* main = dynamic_cast<Main*>(entry))  { return main;  }   }}Dynamic_cast isn't very fast AFAIK, but does the job; make sure to not forget and catch the exception when it fails (ie. when entry class is NOT "main" or a child of) when you use references:void my_function(A& my_a){try{B& my_b = dynamic_cast<B&>(my_a);my_b.methodSpecificToB();}catch (const std::bad_cast& e){std::cerr << e.what() << std::endl;std::cerr << "This object is not of type B" << std::endl;}}void my_function(A* my_a){B* my_b = dynamic_cast<B*>(my_a); if (my_b != 0)my_b->methodSpecificToB();elsestd::cerr << "This object is not of type B" << std::endl;  }from http://en.wikipedia.org/wiki/Dynamic_cast;  dynamic_cast isn't horrifically slow, either. If you're using it in a tight inner loop, it might become problematic (especially with MSVC's implementation which seems a bit brain-dead) but as always, don't make premature optimizations. Write the code in a sensible way first, then worry about it if and only if testing and profiling show performance to be an issue.   ;Quote:Original post by ApochPiQWrite the code in a sensible way firstI don't consider asking for type to be sensible in the first place.It there is one such type per container, then making it explicit avoids all of the above mess altogether.;  Thanks for all your answers! I got trouble with dynamic_cast (Speed is not a problem here, btw) and solved it another, better way. dynamic_cast wanted to have a full definition of that class, and that messed up all my code (#include trouble ... :( ). There is a number that gets saved now, which holds the index. I didn't wanted this at first, because of the trouble with old files, but this is also not a problem anymore :)   ;  Why do you need to find elements of a particular type? So that you can do something specific with them?The entire reason for putting them into a container with elements of a base type is to say that the specific type does not matter - that you only care about the base type. If that isn't true, then they almost certainly shouldn't be in the same container together.Have you considered just making more use of polymorphism? That is, for whatever it is you want to do specifically with Main instances, make a virtual function for that process (and e.g. have it do nothing in the base Object)?   ;Quote:Original post by ZahlmanHave you considered just making more use of polymorphism? That is, for whatever it is you want to do specifically with Main instances, make a virtual function for that process (and e.g. have it do nothing in the base Object)?Oh, it is! Every class in this container extends the one base class: "Object". I was making mods for Unreal before, and I found this system really nice, so I copied a bit from it.The reason why everything is in one container here is, that it is much easier to handle them. But let me explain a bit more:Every object the mapper creates in my engine does not get saved in the mapfile, but in the packages. So, for a mesh, the filechunk, which holds the data would look like this:---------Data section:#Imported MeshData---------Object section:#Mesh-Interface (Holds information, such as shaders, child of Object)#Mesh-Component (Part of my component based entity system, child of Component, wich is a child of object)#Actor (Simple actor, which can be a target for a content-mark, child of Object)So, when loading, the component finds the mesh interface, and the actor finds the component and everything is fine, and ready. But since this is only inside a package, the mapper can't do anything with it. And creating a new package entry for each mesh is also not a good idea. So there are "ContentMarks" which can hold only 4 properties: Location, Rotation, Scale and the Target. The Target is a pointer to an Actor inside a loaded filechunk of a package. Everything is fine here. But now, the mapper wants to place a ContentMark into the world. Ok, lets look what we have selected in the browser. And now, I need to search the list of objects. Is there any Actor, which can be linked to the new ContentMark? Or is it just a texture for example, and can't be placed? If we found some Actor, use that, if not, reject the try to place it.The reason why the placeable actor is not always at point 0 of this list, is that I rather need access to the mesh-interface, or the shader one as to the actor.I hope it is now more clearly what I want to do. :) For now I use just an ugly solution, since including the Actor-header (dynamic_cast wants that) in the filechunk-header just messes up my whole code. Says, that some classes weren't defined and such.