Solved

instanceof in C++

Posted on 2000-03-08
6
3,282 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
iSeries DB2 Query 2 91
substring method in java 1 98
Capture logon name 13 46
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

895 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now