Solved

instanceof in C++

Posted on 2000-03-08
6
3,281 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 85
Expand LInux Boot partition remotly 3 78
Folder Comparison 12 52
fizzArray  challenge 1 49
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: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 video discusses moving either the default database or any database to a new volume.

760 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

20 Experts available now in Live!

Get 1:1 Help Now