473,396 Members | 2,030 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

typeid and inheritance

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
11 6342
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
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
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
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
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


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
>
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
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
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


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
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: Andreas Sch. | last post by:
Hello, I had been quite suprised by the following little program: ---- cut ---- #include "iostream.h" class base {
11
by: Jamie Burns | last post by:
Hello, I just did a simple benchmark: for (xx=0;xx<100000;xx++) { rDerived* derived = dynamic_cast<rDerived*>(object); if (derived) derived->setValue(message.data.messageSetInt.value); } ...
3
by: Mike | last post by:
I want to use typeid() in a base class function to determine the name of the derived class. typeid(this) returns the name of the base class (which is an abstract class) rather than the derived...
3
by: Max | last post by:
I am trying to find a way to eliminate vararg functions from my code by packaging the input parameters in stringstreams. Here is an oversimplified example of what I am trying to do: Functions...
19
by: Marco Jez | last post by:
Hi everyone! I would like to use the reference returned by typeid as key in a std::map. Is it safe to assume that typeid(T) (where T is a type name) will always return the same reference to the...
18
by: Adam Zimny | last post by:
This is fragment of code from Bruce Eckel's Thinking in c++ ( last 3 couts are mine to show what happened ). The question is: is Bruce Eckel wrong or g++ ( my version is 3.2.3 ) is buggy ? //:...
5
by: nobody | last post by:
With Visual C++ 2005 beta 2, I get the below compling error for the following code. I think this error is not acceptable to me because int::typeid is a constant and is known to compiler when...
6
by: ma740988 | last post by:
I was trying to garner a feel for typeid and it's use with polymorphic types #include <iostream> template <class T> bool is_polymorphic() { bool result(false); typeid( (result=true),...
7
by: Deepak Jharodia | last post by:
I'm using a templatized class in GCC based environ template<class A, class B> class foo {... ....} F; Now I want to know that particular instance of this class was instantiated with what...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.