# Does C++ have its own version of Java's InstanceOf

## Recommended Posts

I think the subject says it all, but I have not got a clue how to tell if an object is an instance of class like Java, u write if (object instanceOf ClassA) { } I dont know if C++ has a way. Thanks for the reply DarkStar UK

##### Share on other sites
Not really.

If it's a type with at least one virtual method you can attempt to dynamic_cast to the type in question; if the source object is the same as (or derived from) the target type then you will get a valid pointer, else you will get a null pointer.

But if not, there's no way of really knowing.

##### Share on other sites
Yeah, it's called Run-Time Type Information (RTTI). So the equivallent to your Java would be...
if ( typeid ( object ) == typeid ( ClassA ) ){}

p.s.
I might as well say it now, since I'm sure someone else most likely will, using RTTI is generally considered 'bad' (unless you just can't work around it). I'm sure you knew that though, just getting the inevitable out of the way.

##### Share on other sites
typeid(object)
it requires #include <typeinfo>
Edit: Someone beat me to it

##### Share on other sites
Hmm - can you use RTTI only on some classes and leave others non-RTTI? If so, then I don't see why its a bad form thing. Things like this (plus the painful template system) are why I prefer languages where the classes themselves are objects. Sure, its a little more bloated, but it makes meta-class level work so much more smooth.

##### Share on other sites
But instanceof _always_ works. typeid() does not. typeid() is not guaranteed to work when using derived types that have no virtual members (which can be a legitimate thing to do). An equivalence can't really be drawn.

And if you're going to use typeid() you might as well just use dynamic_cast. dynamic_cast will work even in boneheaded compilers that default to RTTI being disabled.

##### Share on other sites
I think you could use typeid for that.

if (typeid(object) == CObjectType)
{
}

I don't think its performance is particularly good, but neither is Java's instanceOf.

Edit: Beaten, sorry.

##### Share on other sites
Quote:
 If so, then I don't see why its a bad form thing

Because a lot of the time--not all of the time, to be sure, but quite a lot of it--the instanceof (or other RTTI mechanism) is being used to do the job of a virtual function, just in a less efficient and harder to maintain way. As such it tends to set off alarm bells.

##### Share on other sites
Hi

i am not really knowing Java but maybe RTTI(Run Time Type Identification) is hat you are looking for.

typeid(ClassInstance).name()

or

typeid(ClassInstance1) == typeid(ClassInstance2)

you have to include <typeinfo>

by

##### Share on other sites
You can always do RTTI by yourself;

enume RTTI {rttiA, rttiB, rttiC};class A{   RTTI rtti;public:   A(void) :rtti(rttiA){}   A(RTTI rtti) :rtti(rtti){}   RTTI instanceOf(void) const {return rtti;}};class B : public A{public:   B(void) :A(rttiB){}};

or similar

but why bother yourself when there is typeid?

##### Share on other sites
If you are only dealing with a few classes that may or may not be of the correct type at runtime, you may just want to "tag" them somehow so that you can check with certainty.

Of course, this may not be an option if you already have a big mess of classes.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628383
• Total Posts
2982372

• 10
• 9
• 15
• 24
• 11