Jump to content
  • Advertisement

Archived

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

Fredric

RTTI... what the heck?

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

I''m into Polymorphism, and I''ve run into a big huge wall! The book that I''m reading through shows an example of RTTI (Run Time Type Identification) and dynamic_cast . In the book, he gives a brief description of it, but doesn''t show the syntax or how it might be used other than the following example: (NOTE) '' = < #include ''iostream.h'' #define NumberHorses 5 enum TYPE { HORSE, PEGASUS }; class Horse { public: virtual void Gallop() { cout << "Galloping...\n"; } private: int itsAge; }; class Pegasus : public Horse { public: virtual void Fly() { cout << "I can fly!\n"; } }; int main() { Horse * Ranch[NumberHorses]; Horse * pHorse; int choice, i; for (i=0; i> choice; if(choice == 2) pHorse = new Pegasus; else pHorse = new Horse; Ranch = pHorse; } // end for cout << "\n"; for (i=0; iFly(); else cout << "Just a horse\n"; } // end for return 0; } On line 42, it has the example of the RTTI and dynamic_cast ... I have NO idea what that is! Could someone please explain this fully how it can be used, what the hell it is, and why it''s in that particular application? I predict that the number of questions I will be asking will come down as soon as my birthday arrives.. but that''s a while away, so can anyone PLEASE help me with this? This is like trying to read Japanese! Programming::~Fredric(const Annoy_Ance)

Share this post


Link to post
Share on other sites
Advertisement
okay, i''ll TRY to explain:
look at the line you have trouble with
Pegasus * pPeg = dynamic_cast'' Pegasus *'' (Ranch);
Ranch is an array of Horse classes.
Now he wants to find out if the element "i" is a Pegasus or a plain Horse. since Horse is the base class of Pegasus, you can try to dynamically cast the Horse pointer to a Pegasus pointer. if the call succeeds (i.e. the Ranch[i] is a Pegasus, that is derived from Horse, and not a plain Horse) the pointer pPeg becomes valid (!=NULL)
so he can call
pPeg->Fly();

hope that is at least a bit understandable

Share this post


Link to post
Share on other sites
What happens in memory when you derive classes? as in your example, the Pegasus class also has a Horse object as part of its memory.


This is what a Pegasus object looks like (conceptually):

-----------------------------------------
/ /Horse/Pegasus/ /
-----------------------------------------


Here''s what a Horse object looks like in memory:

----------------------------------------------
/ /Horse/ / /
----------------------------------------------


It''s kind of like a really bad symbolism, but a Horse is just a plain horse, while a Pegasus is a horse with something added on -- wings.

So, when you create a Horse object, like so:

Horse graymare;

you have only created a Horse object. But, when you create a Pegasus object, like so:

Pegasus unicorn;

the unicorn has both a Horse object, and also a Pegasus object. When you tell the compiler to do a dynamic_cast on the unicorn from a Pegasus to a Horse, it just moves the pointer to the Horse part of the unicorn.

If you have a Horse pointer to the unicorn, and try to dynamic_cast it to a Pegasus pointer, the compiler looks for the Pegasus part of that object. If it can''t find the Pegasus part, it just returns NULL because you had originally created it to be a Horse.

On the other hand, if the compiler did find the Pegasus part, then it knows that you originally created the object as a Pegasus.

So, you don''t have to know exactly whether you''ve got a Horse or a Pegasus --> you call the Fly() function only if it''s a Pegasus.

I normal game programming, you''ll probably never need to use RTTI -- from what I hear it''s kind of slow. But every language feature is another tool in your pocket if you learn it!

Good Luck!


- null_pointer

Share this post


Link to post
Share on other sites
Oh, thanks for the help you to. It makes sense now... well, kind of. I know how to use it, and how it is used, but people that''ve talked to about this say it''s almost useless... ah well, I''ll take your word null_pointer- I''ll learn it. Ya never know- it might come in handy someday!

Programming::~Fredric(const Annoy_Ance)

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!