c++ equivalent of 'instanceof' ?

Started by
12 comments, last by Zahlman 19 years, 2 months ago
grr...this seems like it's a lot easier than i'm making it i have some classes set up something like abstract class 'baseclass' has subclasses 'sub1' and 'sub2' etc, and i have a function that takes baseclass* as an arg...and it needs to know what kind of 'baseclass' it is. how would i do this in c++? thanks in advance
Advertisement
Use typeid for exact object type comparison, or dynamic_cast<> for more general comparisons. (More similar to instanceof, dynamic_cast<> also checks the inheritance hierarchy.)

Or you could write your own RTTI system with a static member and a couple of virtual functions.
Maybe what you want is typeof. If the compiler that you use doesn't implement that part of the language, check out the implementation found here. It's too bad the articles from CUJ that go along with that implementation aren't posted there as well.

[Edited by - LessBread on February 11, 2005 3:25:27 AM]
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I'm thinking that link isn't correct.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by h2mprod
grr...this seems like it's a lot easier than i'm making it

i have some classes set up something like

abstract class 'baseclass' has subclasses 'sub1' and 'sub2'

etc, and i have a function that takes baseclass* as an arg...and it needs to know what kind of 'baseclass' it is. how would i do this in c++? thanks in advance


Not to be rude or anything, but you're probably doing something wrong if you need to know the type of a derived object. Polymorphism was invented to get around needing to know that information. There is probably a better way of doing what you are doing. If you post more specific details about your class architecture, some people might be able to give you constructive criticism.
I know you asked for "c++ equivalent of 'instanceof' " but you could simply and easily achieve this by using static ints in the classes to give them an id. Then when you need to figure out what they are, just compare the ids. Quick example:

class Base {protected:    static int id;public:    Base() { id = -1; }    int GetID() const { return id; }};class Der1 : public Base {public:    Base() { id = 0; }};class Der2 : public Base {public:    Base() { id = 1; }};


Old-school, but it should work for your needs. Simply call GetID() with any pointer of a *Base class and you will obtain the correct id. You could take this further and even setup an ENUM for the types of classes if you wanted to for more organization. Heck, you could even go all out and use strings. Just an idea though.

- Drew
Quote:Original post by cwhite
Quote:Original post by h2mprod
grr...this seems like it's a lot easier than i'm making it

i have some classes set up something like

abstract class 'baseclass' has subclasses 'sub1' and 'sub2'

etc, and i have a function that takes baseclass* as an arg...and it needs to know what kind of 'baseclass' it is. how would i do this in c++? thanks in advance


Not to be rude or anything, but you're probably doing something wrong if you need to know the type of a derived object. Polymorphism was invented to get around needing to know that information. There is probably a better way of doing what you are doing. If you post more specific details about your class architecture, some people might be able to give you constructive criticism.


basically, i want the special weapon to affect every type of enemy except for the boss. so i need to know if it's a boss object or not before deciding to explody. guess i could add a flag to each enemy to say whether or not it should be affected by the special weapon, but learning new things can never hurt

the arch is like

ship (abstract) -> enemy (concrete) -> boss
There are lots of options for what you're doing:

1) Add a data member or flag to the base class.

2) Add something like a "virtual bool isBoss()" method. This doesn't waste any more memory if you have other virtual functions.

3) Add a "virtual BossInterface * getBossInterface()" method. Return NULL when it's not a boss. This is like isBoss(), but allows you to have a more flexible inheritance hierarchy. It may save you a lot of code duplication if bosses have similar abilities to normal types of enemies.

4) Assign the responsibily of damaging to the object itself. This is the best option, because it'll require far fewer changes if you make a new damage type or enemy type with special skills (eg half damage by freeze missiles when it's facing them).
Quote:Original post by smart_idiot
I'm thinking that link isn't correct.


You are correct. How bizarre! Here's the intended link: User's Guide to GNU C++ - Typeof
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
If you really want something like instanceof in c++, I suggest you use typeid. Typeof is a GNU extension and thus isn't portable.

However h110k pointed out alternatives:

1. Using public datamembers is not generally a good idea.

2. Using something like virtual bool isBoss() is the easy way to go. If you just want to quickly get things done, you can use this way, but if you're still at the drawing board, it's better to look at 4.

3. * getBossInterface() method is just a specific way of doing dynamic_cast and I would advice agains this.

4. Having your entities handle damage themselves is probably the best choice. However this method requires serious reconsideration of your design. (Which is good.) You really need figure out to how your objects will interact in your game and base your interface design on that.

This topic is closed to new replies.

Advertisement