Solved

instanceof in C++

Posted on 2000-03-08
6
3,285 Views
Last Modified: 2013-12-26
In java, I can check the instance of an instance by using the keywork instanceof.
Do C++ in unix have the similar operation?
0
Comment
Question by:matchz
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 2600720
In C++, you'd usually use RTTI and the 'typeid()' operator:

typeid( type-id )

typeid( expression )

The typeid operator allows the type of an object to be determined at run-time.

The result of a typeid expression is a const type_info&. The value is a reference to a type_info object that represents either the type-id or the type of the expression, depending on which form of typeid is used.

The type_info class describes type information generated within the program by the compiler. Objects of this class effectively store a pointer to a name for the type and an encoded value suitable for comparing two types for equality or collating order. The encoding rules and collating sequence for types are unspecified and may differ between programs.




class type_info {
public:
    virtual ~type_info();
    int operator==(const type_info& rhs) const;
    int operator!=(const type_info& rhs) const;
    int before(const type_info& rhs) const;
    const char* name() const;
    const char* raw_name() const;
private:
    ...
};

Note   You must include the typeinfo.h header file in order to use the type_info class.

The problem is that this can't be a 'general answer', as it's compiler dependant...
0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 2605729
Hmm, since you didn't post any contradictions on the above, I'll repost as an answer...
0
 

Author Comment

by:matchz
ID: 2607061
While I cannot find the manual of class typeinfo in Unix, I found it in Win32.
And Thank you for your answer.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:matchz
ID: 2607063
Sorry for late response, while I am busy on somethings in my university.
0
 
LVL 86

Expert Comment

by:jkr
ID: 2608694
>> while I am busy on somethings in my university

Take you time, I like remember enjoying these days  (originally graduated as an engineer in tech. physics)
0
 

Expert Comment

by:Saeven
ID: 7516972
While jkr was right,

If your question was a result of inheritance problems where you are storing base class casted items into a container ( ie. Apples and Oranges being stored into a vector < Fruits > where Apples : Fruits and Oranges : Fruits ), then the nicer thing to do is to use virtual functions to perform the tasks you would have performed with the casted object once you'd have figured out what kind of object it really was.

For example, in Java if you were thinking of something like:

if( instanceof Orange ){
    (Orange)object.print();
}
else if( instanceof Apple ){
    (Apple)object.print();
}

you just do this:

In your base class Fruit, include the following:

virtual void print() = 0;

and then inside each of Apple and Orange, define the following:

virtual void print(){ ... perform your routine ... };

Just an aside that might help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question