RTTI... what the heck?
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; i); // ????
if (pPeg)
pPeg->Fly();
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! </i>
Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
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 is a Pegasus, that is derived from Horse, and not a plain Horse) the pointer pPeg becomes valid (!=NULL)<br>so he can call<br>pPeg->Fly();<br><br>hope that is at least a bit understandable<br> </i>
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 is a Pegasus, that is derived from Horse, and not a plain Horse) the pointer pPeg becomes valid (!=NULL)<br>so he can call<br>pPeg->Fly();<br><br>hope that is at least a bit understandable<br> </i>
I''m still confused...
Programming::~Fredric(const Annoy_Ance)
Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
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
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
argh! the board messed up my ASCII art...well, never mind it...
- null_pointer
- null_pointer
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)
Programming::~Fredric(const Annoy_Ance)
3D Math- The type of mathematics that'll put hair on your chest!
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement