473,761 Members | 10,276 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

c++ version of instanceof()

What is the c++ version of instanceof()?

Thank-you
Sep 10 '05 #1
5 71021
Beginner wrote:
What is the c++ version of instanceof()?


No. There is 'typeid' operator. Let me ask, why do you think
you need it?

V
Sep 10 '05 #2
GB
Beginner wrote:
What is the c++ version of instanceof()?


In C++, runtime type checking is done using typeid and dynamic_cast. See

http://tinyurl.com/8j5dp

http://msdn.microsoft.com/library/de...nformation.asp

Gregg
Sep 10 '05 #3
* GB:
* Beginner:

What is the c++ version of instanceof()?


In C++, runtime type checking is done using typeid and dynamic_cast. See

http://tinyurl.com/8j5dp


Well, yes.

One must assume "Beginner" is asking about something akin to Java's
instanceof, which, first to be said, should be _avoided_, e.g. <url:
http://www.javapractic es.com/Topic31.cjp>.

In Java, the expression

o instanceof T

is true if and only if o is dynamically an instance of a class that directly
or via an implemented interface (1) is T or (2) is derived from T.

In C++ the question is not one of what o is but one of what o can be converted
to, which is already rather different from Java. And one has several
possibilities for both o and T. E.g., is o a pointer or a reference, what is
the cv-qualification of o, does the static type of o support RTTI, what is the
cv-qualification of T? So there is no single equivalent expression.

The simple answer is therefore that there is no "the c++ version" of Java
instanceof.

And a more elaborate answer, that C++ built-in support for runtime type
checking, generally called RTTI, is limited to typeid and dynamic_cast.

However, in contrast to Java C++ also has compile time polymorphism, namely
templates, and there the corresponding question is whether T1 is or is a
subclass of T2. And for a discussion of that, see Andrei Alexandrescu's
"Modern C++ Design". Or, the Boost or Loki library source code.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Sep 10 '05 #4
In addition, please note that only classes that implements at least one
virtual member function can be used for RTTI.

Ben
Sep 10 '05 #5
You probably meant dynamic_cast, not RTTI. typeid works for all the
classes and basic types.

Sep 11 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
13126
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 use that data to populate the controls when the child page is opened again. One of these objects contains an Array, and the page reloads fine using myArray, myArray, etc... But when I try perform some array methods on it (i.e.slice) ... it does...
7
4129
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.
4
1916
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
2180
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, right ?)
2
1688
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 true Can anyone explain why when BOTH 'a' and 'Object' are functions,
15
3431
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 classes: public abstract class AInstruction extends Instruction & public abstract class LocVariableInst extends Instruction please advise me...thankssss
13
2490
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 instanceof where a component extends an object and implements some listener interface too for a MVC architecture.
30
4701
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 author states that the instanceof operator computes its result
3
2139
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
9531
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9345
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9957
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9905
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8780
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7332
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6609
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5373
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3881
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.