"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