0

How can I check the class type in c++?
In Java I used instanceof.

I prefer not to use dynamic cast, but only classic c++.
Is there any way?

Clarification:
It isn't a duplicate of another question in StackOverflow, since I asked how can I find it without using of dynamic_cast. In the other question, the answer was to use it. Please don't mark it as a duplicated.

Community
  • 1
  • 1
Aharon
  • 117
  • 1
  • 1
  • 14
  • 3
    What do you mean by classic C++ if not dynamic_cast? And what are you actually trying to do? – chris Jul 04 '13 at 07:06
  • I mean, for `dynamic_cast` you need use `std::...`. For any reason I have not to use it. I created base class and some derive ones. When I get an object I want to do an operation associated to the specific derived class, but I got it in a loop so it is defined as the base one. In Java, by `instanceof` I could know which type is it. But in C++ with no use of `dynamic_cast`, how can I know? – Aharon Jul 04 '13 at 07:30
  • `dynamic_cast` is a language feature, not a library feature. It really sounds like you just want a virtual function that the derived class overrides. – chris Jul 04 '13 at 07:36
  • You right, but I want to know it without using of virtual function. Only to check type. – Aharon Jul 04 '13 at 07:51
  • Well, checking the type is generally considered bad design in the first place. There's a reason C++ doesn't have it and you wouldn't need to check the type and do something based on it with a virtual function in place. – chris Jul 04 '13 at 07:54
  • I'm not sure I understand: you want to check the type, but you don't want to use `dynamic_cast`, which is the way C++ checks the type. (In Java, `instanceof` is just a semantic wrapper for the equivalent of `dynamic_cast` in C++.) – James Kanze Jul 04 '13 at 09:03
  • @JamesKanze Yes, you understand perfectly. I have a problem in use of `std` namespace and search for aletrnative way. If I wrote it in Java, I wouldn't have this problem. Java is a strong language. – Aharon Jul 04 '13 at 09:18
  • @Aharon: If you remove `Object` and the other library classes in Java, it's not a strong language either. The stuff _outside_ `std::` is mostly there for C compatibility, and C is not an OO language. So asking for OO features, and restricting yourself to the non-OO subset of C++ is plain dumb. – MSalters Jul 04 '13 at 09:56
  • @Aharon First, `dynamic_cast` is *not* in `std`. It's a keyword. Second, as MSalters says, removing `std` from C++ is the equivalent of removing the package `java` from Java. And what would Java be without `java.lang.Object` or `java.lang.String`? – James Kanze Jul 04 '13 at 10:05

1 Answers1

2

There is no way to check class type without RTTI or it's home brew substitution. If application compiled without RTTI information about type is not stored anywhere.

alexrider
  • 4,449
  • 1
  • 17
  • 27