Solved

instanceof in C++

Posted on 2000-03-08
6
3,283 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
  • 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
string initialization in java 11 113
centeredAverage challenge 8 161
NotAlone Challenge 20 83
matchUp  challenge 9 105
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…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

808 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