* GB:
* Beginner:
What is the c++ version of instanceof()?
In C++, runtime type checking is done using typeid and dynamic_cast. See
http://tinyurl.com/8j5dp
Well, yes.
One must assume "Beginner" is asking about something akin to Java's
instanceof, which, first to be said, should be _avoided_, e.g. <url:
http://www.javapractic es.com/Topic31.cjp>.
In Java, the expression
o instanceof T
is true if and only if o is dynamically an instance of a class that directly
or via an implemented interface (1) is T or (2) is derived from T.
In C++ the question is not one of what o is but one of what o can be converted
to, which is already rather different from Java. And one has several
possibilities for both o and T. E.g., is o a pointer or a reference, what is
the cv-qualification of o, does the static type of o support RTTI, what is the
cv-qualification of T? So there is no single equivalent expression.
The simple answer is therefore that there is no "the c++ version" of Java
instanceof.
And a more elaborate answer, that C++ built-in support for runtime type
checking, generally called RTTI, is limited to typeid and dynamic_cast.
However, in contrast to Java C++ also has compile time polymorphism, namely
templates, and there the corresponding question is whether T1 is or is a
subclass of T2. And for a discussion of that, see Andrei Alexandrescu's
"Modern C++ Design". Or, the Boost or Loki library source code.
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?