Downcasting / Run Time Type Information

Started by
13 comments, last by randomZ 21 years ago
OK, my program structure is as follows: I have the class OnScreenObject. Derived from this is the class ClickableObject. A std::list called "objects" of OnScreenObject*s is managed by a Scene object. Now, if a mouse click is registered, the scene needs to check whether an object is clickable. I do this like that:
  
for(list<OnScreenObject*>::iterator i = objects.begin(); i != objects.end(); ++i)
{
  ClickableObject* co = 0;
  if(co = dynamic_cast<ClickableObject*>(*i))
  {
    // ...

  }
}

However the dynamic cast gives me a compile error.

Why?
  
"George W. Bush Geography Simplification Initiative"
More info on George W. Bush My Homepage (C++ SDL OpenGL Game Programming)
I am a signature virus. Please add me to your signature so that I may multiply.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Advertisement
What compile error do you get?

Another way to handle this is to have a virtual function called IsClickable in your base class, and each derived class can either return true of false depending on whether or not it is clickable. The base class could return false by default just to be safe.
The compile error is only half-visible because of a bug in Dev-C++.

22 E:\Projects\BWInf_2\Brandubh\scene.cpp
cannot dynamic_cast `(&i)->std::_List_iterator<_Tp, _Ref,

Thanks for the advice, I''ll think about it. But first I''ll see if I get more replies =)

"George W. Bush Geography Simplification Initiative"

More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Have you enabled RTTI?


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
whaddayamean enabled? Where do I enable that? Shouldn't dynamic_casting a pointer, if invalid, always return 0? It's in Stroustrup's book...

EDIT: Stroustrup not Stroustroup...

"George W. Bush Geography Simplification Initiative"
More info on George W. Bush
My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

[edited by - randomZ on March 30, 2003 2:43:50 PM]
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
In MSVC++ you need to enable RTTI (Run-Time Type Information) for your project to be able to use dynamic_cast, perhaps it is the same in Dev-C++.


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Didn''t find such an option. Would have surprised me, too

"George W. Bush Geography Simplification Initiative"
More info on George W. Bush

My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/
Ensure that the -fno-rtti flag is not set when building your project.

Does dynamic_cast work in other parts of your code?


Update GameDev.net system time campaign - success at last

[edited by - dalleboy on March 30, 2003 2:58:38 PM]
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
BTW, does this work?

  for(list<OnScreenObject*>::iterator i = objects.begin(); i != objects.end(); ++i){   OnScreenObject* oso = *i;   ClickableObject* co = dynamic_cast<ClickableObject*>(oso);   if(co)   {      // ...   }}  



Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
dalleboy: Your code example gives a similar error message.

I can't find a -fno-rtti flag anywhere (in the Makefile, the project options and the compiler options).

The following upcast compiles without an error:

    ClickableObject* bla = 0;OnScreenObject* asd = dynamic_cast<OnScreenObject*>(bla);    

Yes, this code serves no purpose. It's just an example =)

EDIT: Wrote "*second* code example" in the first line... why??? Sometimes I'm confused :D

"George W. Bush Geography Simplification Initiative"
More info on George W. Bush

My Homepage (C++ SDL OpenGL Game Programming)

I am a signature virus. Please add me to your signature so that I may multiply.

[edited by - randomZ on March 31, 2003 1:11:14 PM]
---Just trying to be helpful.Sebastian Beschkehttp://randomz.heim.at/

This topic is closed to new replies.

Advertisement