Sign in to follow this  

c++ equivalent of 'instanceof' ?

This topic is 4692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
BTW, typeof and typeid are orthogonal in the sense that the former is strictly a compile-time operator, the latter run-time. They cannot be used interchangeably; typeof returns the actual static compile-time *type* of its operand, while typeid returns an *object* containing some information about the run-time polymorphic type of its operand.

So, typeof certainly won't help here, and using typeid in this case, while getting the work done, is a lot uglier than using dynamic_cast (which of course isn't the most elegant thing to use in the first place!) As others have said, certainly the best option is to use normal polymorphism by moving the damage handling to the entities themselves.

Share this post


Link to post
Share on other sites
I was under the impression that typeof was a generally unimplemented part of the standard, that was incorrect. Perhaps it's something slated for the next version of the standard. At any rate, I was also mistaken about the presence of the articles accompanying the code for the portable typeof operator. Here are pdfs of the articles from the C/C++ Users Journal.

part 1
part 2
part 3

Share this post


Link to post
Share on other sites
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.


A bit of C++ mythology here I believe, just because there isn't any elegant way of doing it in C++ doesn't mean it isn't useful.

Share this post


Link to post
Share on other sites
Quote:
Original post by psamty10
Quote:
Original post by cwhite
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.


A bit of C++ mythology here I believe, just because there isn't any elegant way of doing it in C++ doesn't mean it isn't useful.


In this case it's not "C++ mythology"; more like "OO dogma". :)

Share this post


Link to post
Share on other sites

This topic is 4692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this