1

I would like to know if it is possible to do something like the "instanceof" of Java in C++. Indeed I searched a lot but I have not found anything for finding the class of an instance of a class.

To sum up, I would like to use something like this:

bool instanceOf(Gui guiA, Gui guiB)
{
    Gui *castedGuiPtr = dynamic_cast<classOf(guiA)*>(guiB);
    return castedGuiPtr != 0;
}
Benjamin Loison
  • 3,782
  • 4
  • 16
  • 33
  • 3
    What for? What actual problem are you trying to solve? – spectras Aug 05 '17 at 01:15
  • I am looking for a function: classOf(instanceOfAClass) – Benjamin Loison Aug 05 '17 at 01:18
  • My apologies. Misread your goal. Still doesn't exist, but I don't have a good alternative solution for you. – user4581301 Aug 05 '17 at 01:25
  • This is almost certainly the wrong thing to do, and there is no general case, but just for fun you could do this: http://ideone.com/PpNiLi – user4581301 Aug 05 '17 at 02:33
  • What is "OP" in your comment ? – Benjamin Loison Aug 05 '17 at 04:09
  • @BenjaminLOISON> a function is a **tool**, not a problem. Because C++ is not Java, it does not use the same tools that Java uses to solve problems. This is why I asked what problem you are trying to solve: to point you at the proper C++ tools for your problem. Merely looking in C++ toolbox for the Java tools you know will get you nowhere. – spectras Aug 05 '17 at 10:21
  • @user4581301 Ok but could you tell me what is "OP" ? If it is an abbreviation ? ... Thank you – Benjamin Loison Aug 05 '17 at 13:57
  • Original Poster: abbreviation for the originator of a forum thread. Since this is not a forum, OP is being mis-used. I should probably have used "Asker" as in asker of the question. – user4581301 Aug 05 '17 at 15:40
  • The thing you should take away from this is you can brute-force behaviour similar to Java in C++, but is isn't easy, it isn't quick, and it is error prone. If you think this is the solution to your problem, you are thinking about your problem wrong. You're obviously not new to programming in general, so you may find Scott Meyers's Effective C++ (Third edition) helpful in migrating to C++. – user4581301 Aug 05 '17 at 15:46
  • @user4581301 Ok thanks a lot. I solved my problem by generating at initialization pointers to my guis and then I have a pointer to use a special child gui and when I change the actual gui, I change a string to the name of the new gui. This way my problem is not very fine solved but it is. https://pastebin.com/iR5CcGTY just need to use: if(actualGui == "GuiWanted") as an "instanceof" in Java – Benjamin Loison Aug 06 '17 at 05:24

0 Answers0