By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,897 Members | 1,955 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,897 IT Pros & Developers. It's quick & easy.

instanceof in C++?

P: n/a
Hi All,

Is there a function similar to Java's instanceof in C++?
To ask the question a different way, if I have a base class and two derived
classes, how can I tell which one of the derived classes a particular object
belongs to?

BaseClass* base;

if(some_test){
base = new Class1;
} else {
base = new Class2;
}

How can I tell if I created a Class1 or Class2 object from the object?

Thanks for your help

Michael
Mar 16 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Mar 16, 10:19 am, "Michael" <michael5...@yahoo.comwrote:
Hi All,

Is there a function similar to Java's instanceof in C++?
To ask the question a different way, if I have a base class and two derived
classes, how can I tell which one of the derived classes a particular object
belongs to?

BaseClass* base;

if(some_test){
base = new Class1;} else {

base = new Class2;

}

How can I tell if I created a Class1 or Class2 object from the object?

Thanks for your help

Michael
dynamic_cast is your friend.

Cheers
Marc

Mar 16 '07 #2

P: n/a
On 3/16/2007 10:19 AM, Michael wrote:
Hi All,

Is there a function similar to Java's instanceof in C++?
To ask the question a different way, if I have a base class and two derived
classes, how can I tell which one of the derived classes a particular object
belongs to?

BaseClass* base;

if(some_test){
base = new Class1;
} else {
base = new Class2;
}

How can I tell if I created a Class1 or Class2 object from the object?
Use dynamic_cast< :

if(Class1* c1 = dynamic_cast<Class1*>(base))
// it's a Class1*
else if (Class2* c2 = dynamic_cast<Class2*>(base))
// it's a Class2*
But: Frequent use of dynamic_cast<is a smell of bad design!
S.
--
Stefan Naewe
stefan_DOT_naewe_AT_atlas_DOT_de
X-Replace-Address: yes
Mar 16 '07 #3

P: n/a

"Michael" <mi*********@yahoo.comwrote in message
news:45***********************@per-qv1-newsreader-01.iinet.net.au...
Hi All,

Is there a function similar to Java's instanceof in C++?
To ask the question a different way, if I have a base class and two
derived classes, how can I tell which one of the derived classes a
particular object belongs to?

BaseClass* base;

if(some_test){
base = new Class1;
} else {
base = new Class2;
}

How can I tell if I created a Class1 or Class2 object from the object?
You could use 'dynamic_cast', but the whole point of derivation
(with a reasonable design), is that you should not *need* to know
which derived type it is. From an interface standpoint, both
your instances are type 'base' (Isn't that why you used type 'base*'
with 'new'?). If there were a situation where the derived type is
significant,
I'd think you'd write:

Class1 *c = new Class1;

-Mike
Mar 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.