Downcasting / Run Time Type Information

Started by
13 comments, last by randomZ 21 years ago
What is the error message?


Update GameDev.net system time campaign - success at last

[edited by - dalleboy on March 31, 2003 3:07:59 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]
Advertisement
Hmm, I started make through the command line, and the full message is a bit more useful:

scene.cpp:21: cannot dynamic_cast `oso'' (of type `class OnScreenObject*'') to type `class ClickableObject*'' (source type is not polymorphic)

But... since I derived something from OnScreenObject, it *is* polymorphic, isn''t it?
Here''s the def:


  class OnScreenObject{  private:  public:    int x, y;    SDL_Surface* img;    void load(const string &fname);        OnScreenObject(void)    : img(0),      x(0), y(0)    {  }};class ClickableObject : public OnScreenObject{  private:  public:    void (*onClick) (void);        ClickableObject(void)    : onClick(0)    { OnScreenObject::OnScreenObject(); }};  


BTW, the call of OSO''s constructor in the CO constructor is correct, right?

So, what stops me from downcasting here?

"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/
You probably need a virtual member function. The easiest approach is to make the destructor for OnScreenObject virtual.

It is a Good Thing™ to make the destructor of a class virtual:
- If you are going to derive from the class.
- If it contains virtual member functions.
- If you are going to use dynamic_cast on it (as some compilers use the vtable to do the dynamic_cast).

EDIT1: destrutor -> destructor. Added last item in list.


Update GameDev.net system time campaign - success at last

[edited by - dalleboy on April 1, 2003 5:25:49 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]
quote:Original post by randomZ
BTW, the call of OSO''s constructor in the CO constructor is correct, right?

Nope, what you are doing is creating a temporary OnScreenObject on the stack in the ClickableObject constructor.

  ClickableObject(){   // The following will create a temporary OnScreenObject   // on the stack, that will be deleted at the end of the   // statement.   OnScreenObject::OnScreenObject();}  


  // The following will explicitly call the constructor of// the base type.ClickableObject() : OnScreenObject(){}  


  // The following will implicitly call the constructor of// the base type.ClickableObject(){}  



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]
Ah, so there''s not even a need to call it explicitly. Very nice

OK, I''ve added an empty, virtual constructor to both classes... it works perfectly now. Thank you
And it even immediately started detecting clicks correctly, so the (untested) code I wrote before the last successful compile was actually correct!!! Wow, this gives me a BOOST of motivation!!!


I''m so happy...

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

This topic is closed to new replies.

Advertisement