473,378 Members | 1,396 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,378 software developers and data experts.

c++ version of instanceof()

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

Thank-you
Sep 10 '05 #1
5 70968
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.javapractices.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
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.
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.