Determining class of an object

F

Fred

Is there a way to determine the class of an object?
For instance, if I have an array of Animal instances, how do I extract
the items that are of subclass type Dog?

Java has the "instanceof" operator, but is there any way in C++ to
do this without each class having an instance variable with some
kind of unique identifier for the class (this would be OK for classes
that I create, but doesn't scale to third-party classses).
 
I

Ian Collins

Fred said:
Is there a way to determine the class of an object?
For instance, if I have an array of Animal instances, how do I extract
the items that are of subclass type Dog?

Java has the "instanceof" operator, but is there any way in C++ to
do this without each class having an instance variable with some
kind of unique identifier for the class (this would be OK for classes
that I create, but doesn't scale to third-party classses).
Look up std::type_info and typeid. The type_info name will be unique,
but the format of the name is not portable.
 
Ø

Øyvind Røtvold

Fred said:
Is there a way to determine the class of an object?
For instance, if I have an array of Animal instances, how do I extract
the items that are of subclass type Dog?

Java has the "instanceof" operator, but is there any way in C++ to
do this without each class having an instance variable with some
kind of unique identifier for the class (this would be OK for classes
that I create, but doesn't scale to third-party classses).

Try dynamic_cast<>, from the back of my head:

Animal*an_animal;
.....
Dog* a_dog = dynamic_cast<Dog*>(an_animal)
if (a_dog)
{
// Do dog stuff
}

This requires pointers and polymorphic classes, and as others have
said, try to use polymorphism rather than checking types like this,
dynamic_cast<> is likely to be a design flaw.
 
J

James Kanze

Look up std::type_info and typeid. The type_info name will be
unique, but the format of the name is not portable.

There's no requirement in the standard that it be unique, and I
don't know of a compiler where it actually is. In general, all
you can count on is that it is unique for all classes defined at
namespace scope in a named namespace or global scope, and that's
just a QoI issue, not guaranteed by the standard. Once you
start dealing with unnamed namespaces, nested classes, and local
classes, all bets are off.
 
J

James Kanze

Ian's advice is fine. I'd like to add my 2 cents to it.
There is no mechanism in C++ that would allow you to utilize
the information you can obtain from 'typeof' operator beyond
comparing it with some other. You can't automagically create
instance of that type, you can't interrogate the 'type_info'
object like you could do that in Java. So, don't get your
hopes up.

You don't have all of Java's reflection built into the language,
but a restricted form of the most frequent and most useful idiom
(Class.forName( "Toto" ).newInstance()) is fairly simple to
implement. You will need a wrapper around std::type_info,
however, in order to use it as an index into an std::map. It's
not copiable, and doesn't support <, although it has a member
function before() which establishes the desired ordering, so you
need a wrapper which contains a pointer to the type_info object,
and implements < in terms of p->before().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top