Sign in to follow this  
Dark Star

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

Recommended Posts

Dark Star    100
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 this post


Link to post
Share on other sites
DrPizza    160
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 this post


Link to post
Share on other sites
microdot    324
Yeah, it's called Run-Time Type Information (RTTI). So the equivallent to your Java would be...

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


Google it for more info.

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 this post


Link to post
Share on other sites
Pxtl    354
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 this post


Link to post
Share on other sites
DrPizza    160
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 this post


Link to post
Share on other sites
andrewo    122
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 this post


Link to post
Share on other sites
DrPizza    160
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 this post


Link to post
Share on other sites
einki    122
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 this post


Link to post
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 this post


Link to post
Share on other sites
smitty1276    560
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this