Solved

instanceof in C++

Posted on 2000-03-08
6
3,284 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

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