Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typeid and inheritance

Reply
Thread Tools

typeid and inheritance

 
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-29-2006
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...

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      12-29-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-29-2006
> 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...

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-29-2006
Gianni Mariani wrote:

> (E-Mail Removed) 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?

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      12-29-2006
(E-Mail Removed) 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.

 
Reply With Quote
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-29-2006
> 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?

 
Reply With Quote
 
andrewmcdonagh
Guest
Posts: n/a
 
      12-29-2006


On Dec 29, 1:07 pm, (E-Mail Removed) 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

 
Reply With Quote
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-29-2006
>
> 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.

 
Reply With Quote
 
Nikolaos D. Bougalis
Guest
Posts: n/a
 
      12-29-2006
(E-Mail Removed) 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 <en36vl$q8m$00$(E-Mail Removed)-online.com> by 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
 
Reply With Quote
 
mscava@gmail.com
Guest
Posts: n/a
 
      12-29-2006
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
thinking in c++ by B. Eckel, typeid and g++ problem. Adam Zimny C++ 19 08-29-2011 04:33 PM
relation between typeid and typeinfo class (related by friend ?) Ganesh C++ 3 05-10-2005 09:53 AM
template and typeid Max C++ 3 06-30-2004 02:59 PM
typeid and dynamic_cast, gcc 3.3 Andreas Sch. C++ 18 01-29-2004 10:24 PM
typeid and polymorphic classes Dave Theese C++ 5 09-08-2003 08:04 PM



Advertisments