473,406 Members | 2,371 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

instanceof in C++?

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
3 14710
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
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

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: John MacIntyre | last post by:
Hi, I have a page with a series of child pages loaded into an iframe. When I move from page to page, I store an object containing the child's control data in a variable on the main page, then...
7
by: chirs | last post by:
Hi, These 2 lines caused an error in IE5. The error is "Function expected". Why? var d=new Date(); document.write(d instanceof Object + "<br>"); Thanks.
5
by: Beginner | last post by:
What is the c++ version of instanceof()? Thank-you
4
by: Sean Inglis | last post by:
Well bizarre to me, anyway. I've distilled it down to two small files: testtop.htm =============================== <html> <head> <script language="Javascript">
2
by: System Administrator | last post by:
Hi: Can someone explain why BOTH of the following are true ? Function instanceof Object //true Object instanceof Function //true (Either one or the other should be true, not both,...
2
by: System Administrator | last post by:
function a() { } typeof a //returns 'function' a instanceof a //returns false typeof Object //returns 'function' Object instanceof Object //returns ...
15
by: shana07 | last post by:
Can I have many instanceof code for one 'if' ? for example..... if(checkMe instanceof AInstruction instanceof LocVariableInst) What I am trying to say is, these instanceof are refer to these...
13
by: Robert | last post by:
Hi, Is it possible to intercept a call to "instanceof" and return something different? I wanted to see if it was possible to implement something like multiple inheritance, especially to use...
30
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The...
3
by: whitelined | last post by:
Hi, How widely supported is the instanceof operator? Is there an alternative to seeing if an object is an instance of a constructor? Many thanks Regards Aaron
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.