Advertisement

derived classes and linked lists

Started by August 15, 2001 10:36 PM
2 comments, last by +AA_970+ 23 years, 6 months ago
ok I want to make a linked lists of all the monsters in my game world, so let''s say I have a base class, CMonster, which has a pointer to the next and previous monster in the list (CMonster *next, CMonster *prev). I want all my monsters to be derived from this class, so let''s say we have CGoblin : public CMonster. Now comes the problem, to cycle through the linked list or set the next and previous monsters in the list I need a pointer of type CMonster, but the monsters will not be of type CMonster, they''re type will be a derived class such as CGoblin. So what do I do? Digital Radiation
That's what virtual functions and polymorphism are for. You don't need to do anything special.


~~~~~~~~~~
Martee

Edited by - Martee on August 15, 2001 11:46:13 PM
ReactOS - an Open-source operating system compatible with Windows NT apps and drivers
Advertisement
ahhh..i remember when I used to think like that. But you only need a pointer to the base class.

Like


CMonster* monster;
monster = new CGoblin;

you can now use monster just like it was a goblin monster
you might have to cast it on some compilers or if your compiler warning level is set real high...like
((CGoblin*)monster)->function();


"I pity the fool, thug, or soul who tries to take over the world, then goes home crying to his momma."
- Mr. T
thanx for the help

Digital Radiation

This topic is closed to new replies.

Advertisement