Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

randomZ

Downcasting / Run Time Type Information

This topic is 5654 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

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.

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!