By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,124 Members | 950 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,124 IT Pros & Developers. It's quick & easy.

typeid and inheritance

P: n/a
Here are my classes. Each of them has virtual destructor:

class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };

and here's a vector of those objects:

vector<Object*objList;
objList.push_back( new ClosedDoor );

Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?

Thanks for replies...

Dec 29 '06 #1
Share this Question
Share on Google+
11 Replies


P: n/a
ms****@gmail.com wrote:
Here are my classes. Each of them has virtual destructor:

class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };

and here's a vector of those objects:

vector<Object*objList;
objList.push_back( new ClosedDoor );

Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?
The best way is to have a virtual method on Object that tells you what
it is.

Second (by a long shot) is to use dynamic_cast.
Dec 29 '06 #2

P: n/a
The best way is to have a virtual method on Object that tells you what
it is.

Second (by a long shot) is to use dynamic_cast.
But still. I can create a method type_info GetTypeInfo() { return
typeid( *this); }. Call objList[0]->GetTypeInfo() and it will return
ClosedDoor. I want it to tell me that ClosedDoor is a Door...

Dec 29 '06 #3

P: n/a
Gianni Mariani wrote:
ms****@gmail.com wrote:
>Here are my classes. Each of them has virtual destructor:

class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };

and here's a vector of those objects:

vector<Object*objList;
objList.push_back( new ClosedDoor );

Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?

The best way is to have a virtual method on Object that tells you what
it is.

Second (by a long shot) is to use dynamic_cast.
Why would adding a virtual member function and overloading it in every
subclass be better than using the language's built-in tool for the job?

Dec 29 '06 #4

P: n/a
ms****@gmail.com wrote:
>The best way is to have a virtual method on Object that tells you what
it is.

Second (by a long shot) is to use dynamic_cast.

But still. I can create a method type_info GetTypeInfo() { return
typeid( *this); }. Call objList[0]->GetTypeInfo() and it will return
ClosedDoor. I want it to tell me that ClosedDoor is a Door...
Try the dynamic_cast, like

if (dynamic_cast<Door>(objList[0]))
std::cout << "Object is a door\n";

But generally, you should avoid needing the type information.

Dec 29 '06 #5

P: n/a
Why would adding a virtual member function and overloading it in every
subclass be better than using the language's built-in tool for the job?
Yes that's true. So there is no built in way in C++ to find out that
class ClosedDoor IS A Door?

Dec 29 '06 #6

P: n/a


On Dec 29, 1:07 pm, msc...@gmail.com wrote:
Here are my classes. Each of them has virtual destructor:

class Object { ... };
class Door : public Object { ... };
class ClosedDoor : public Door { ... };
class OpenedDoor : public Door { ... };

and here's a vector of those objects:

vector<Object*objList;
objList.push_back( new ClosedDoor );

Operator typeid returns class ClosedDoor. But I want to know wheter it
is a Door. Is there a way find this out?

Thanks for replies...
There a few ways, but first a question....

Why do you need to check what the type is?

Asking an object (regardless that we can using built in or home grown
methods) what type it is, is usually a Design Flaw.

'Tell don't ask' is a better safer approach....

door.handleActionEvent(doorEvent);

instead of

if (door.isClosedDoor() )
{
door.close();
}
else
{
door.open();
}

Andrew

Dec 29 '06 #7

P: n/a
>
There a few ways, but first a question....

Why do you need to check what the type is?

Asking an object (regardless that we can using built in or home grown
methods) what type it is, is usually a Design Flaw.

'Tell don't ask' is a better safer approach....

door.handleActionEvent(doorEvent);

instead of

if (door.isClosedDoor() )
{
door.close();
}
else
{
door.open();
}

Andrew
Well the problem is a bit different than example. I'm making an RPG
game.

// Base class for all tiles
class Tile { ... };
// class for all Accesible Tiles
class Accesible : public Tile
// class for all Inaccesible Tiles
class Inaccesible : public Tile

Well and from those 2 subclasses - Acc and Inacc are derived all
classes, even the Door :)
And when generating Map I need to recognize wheter Tile is accesible or
not.

Ehm... I guess it would be OK to have member in tile bool isAccesible.
So this one is solved.

But still I sometimes it'd be useful to know the type it. You see. I
may have few Tile derivates like Grass, Flowers, Sand etc. And it'd be
useful to group them in one class for example Ground. So when I will
need to work with all the Grounds I will not have to write a lot ifs.

Dec 29 '06 #8

P: n/a
ms****@gmail.com wrote:
>Why would adding a virtual member function and overloading it in every
subclass be better than using the language's built-in tool for the job?

Yes that's true. So there is no built in way in C++ to find out that
class ClosedDoor IS A Door?
Yes, as was shown in a post <en*************@news.t-online.comby Rolf
Magnus, you can use dynamic_cast<against an object to perform a cast at
runtime. If the cast is successful (that is, it returns non-NULL) then it
worked. If it returns NULL, it didn't.

-n
Dec 29 '06 #9

P: n/a
Ok... Thank you very much guys for all your replies! I feel like I'm
gonna think more when I'll want to use RTTI. But I'll also try the
trick with dynamic_cast. Thanks.

Dec 29 '06 #10

P: n/a


On Dec 29, 2:48 pm, msc...@gmail.com wrote:
There a few ways, but first a question....
Why do you need to check what the type is?
Asking an object (regardless that we can using built in or home grown
methods) what type it is, is usually a Design Flaw.
'Tell don't ask' is a better safer approach....
door.handleActionEvent(doorEvent);
instead of
if (door.isClosedDoor() )
{
door.close();
}
else
{
door.open();
}
AndrewWell the problem is a bit different than example. I'm making an RPG
game.

// Base class for all tiles
class Tile { ... };
// class for all Accesible Tiles
class Accesible : public Tile
// class for all Inaccesible Tiles
class Inaccesible : public Tile

Well and from those 2 subclasses - Acc and Inacc are derived all
classes, even the Door :)
And when generating Map I need to recognize wheter Tile is accesible or
not.

Ehm... I guess it would be OK to have member in tile bool isAccesible.
So this one is solved.

But still I sometimes it'd be useful to know the type it. You see. I
may have few Tile derivates like Grass, Flowers, Sand etc. And it'd be
useful to group them in one class for example Ground. So when I will
need to work with all the Grounds I will not have to write a lot ifs.

Yes, in this case isAccessable should be a boolean returning query
method on Tile.

As for the rest, it would appear that the Composite pattern* would be a
good design choice.

Tile <--------------------
| |
Wall Grass Flower Ground----

*intended to facilitate building complex (composite) objects from
simpler (component) parts by representing the part-whole hierarchies as
tree-like structures.

The first Google for 'composite pattern c++' gives....
http://www.codeproject.com/useritems/Composite.asp

So in your example, the Ground object would contain a number of other
Tile Objects, the exact type of which Ground does not need to know.
Andew

Dec 29 '06 #11

P: n/a
<ms****@gmail.comwrote in message
news:11**********************@s34g2000cwa.googlegr oups.com...
Ok... Thank you very much guys for all your replies! I feel like I'm
gonna think more when I'll want to use RTTI. But I'll also try the
trick with dynamic_cast. Thanks.
I believe that dynamic_cast requires RTTI to be turned on in the compiler
(at least for MS VC++ 2003).
Dec 29 '06 #12

This discussion thread is closed

Replies have been disabled for this discussion.