MFC, CObList

Started by
6 comments, last by ape 22 years, 11 months ago
I''m learning MFC, tell me if I''m getting this right. 1. I can have a class Enemy, that inherits from CObject. 2. This class Enemy will have a virtual function called doAI() ex class Enemy: CObject virtual void doAI() // does nothing 3. Any number of enemies of different types can inherit from this base class, and each different enemy can have its own function doAI(). ex class CGoblin : public Enemy void doAI() '''' Goblin thinking class CZombie : public Enemy void doAI() '''' Zombie thinking 4. Then I can have a CObList of instances of class Enemy, each of which is-a CObject 5. and then all I have to do is iterate thru the CObList, calling doAI() on each object, and depending on what type of enemy is there, the proper doAI() will be called. ex. CObList myList; CGoblin myGoblin; CZombie myZombie; //mylist is at head position, pointing to null mylist.addhead(myGoblin) mylist.addhead(myZombie) mylist.getat(mylist.getHeadPosition())->doAI() //zombie thinking mylist.getnext()->doAI() //goblin thinking Is all this right?
Advertisement
Little help please?
wow! I''m impressed that there are actually people on these forums that deal with the technical stuff like inherance and linked lists. So congrats on taking the initiative to learn the language instead of saying "I want to make a FPS but don''t know how to program. Please Help."

I haven''t dealt much with inheritance, but from what I can see, you''re using everything properly, (and you''ve got the right idea for processing AI.)

So What''s the problem? Run-time error? Not doing what you want it to do?



"Be that word our sign in parting, bird or fiend!" I shrieked, upstarting —
"Get thee back into the tempest and the Night''''s Plutonian shore!

-just 2 of 96 lines from E.A.P.''s "the Raven"
"Be that word our sign in parting, bird or fiend!" I shrieked, upstarting —"Get thee back into the tempest and the Night''s Plutonian shore!-just 2 of 96 lines from E.A.P.'s "the Raven"
You''ve got the inheritence correct. Couple of questions:
- Why are you inheriting from CObject? It only buys you a couple of things, none of which I think you''re using. If it''s so you can use CObList, then you''d be better off using "CList" instead, which is general.
- Since your Enemy::AI doesn''t do anything, you probably want to make Enemy::AI a pure virtual function. This will prevent you accidentally making an instance of Enemy, and will force all derived classes to fill-in their own AI.

Now, if you use a CList instead of a CObList, you have to be careful to make a CList of pointers to Enemy, not of Enemy. If you make a list of Enemy (instead of *Enemy), you can''t use inheritence; virtual function overrides only work through a pointer or reference.

Try something like this on for size:
  // in stdafx.h#include <afxtempl.h> // needed for CList// in enemy.hclass Enemy{public:	virtual void doAI () = 0; // pure virtual};// in tank.hclass Tank : public Enemy{public:	virtual void doAI () { /* do something */ }};// in main cpp fileCList<Enemy*, Enemy*> m_listEnemy; // list of pointersm_listEnemy.AddTail (new Tank); // add a tankEnemy *pEnemy = m_listEnemy.GetHead ();pEnemy->doAI ();  


thanks guys, that''s what a I needed.

"Take thy beak from out my heart,
And take thy form from off my door!
Quoth the Raven, ''Nevermore''"

more from E.A.P

I use CObject for the serialization.

Start with CDocument, and then use CObject for the rest.

IE:
CWorld :: CDocument
CClan :: CObject
CWarrior :: CObject
CStockPile :: CObject
CTown
CBuilding
CPerson
etc..

Then CWorld can have a list of clans.
Clans can have a list of warriors,
warrior can have lists of items..

And you just put everything in CWorld.

Then you can load and save the world with
a single command.

btw.. CMaps are faster and more useful than than lists..

Woodie Dovich..

You have it correct, sir.

You could derive from CObject or not. Doesn''t really matter, but as you say the archive/serialize is convenient to have sometimes.

base class pointers are sweet... And references work the exact same way, BTW. You can have a list of Object& and it will also end up calling the right virtual functions based on what type the derived class object ''really is.''

Woodie! Whassup! (grin)

Douglas Sutherland

Not sure why this post was resurrected from many moons ago, but..

quote:Original post by Anonymous Poster
btw.. CMaps are faster and more useful than than lists..


Are you smoking crack? Lists and maps (a.k.a. dictionaries a.k.a. hash tables) are completely different containers and have completely different attributes, including that one is sequential and one is sorted. I hope you were joking.




Edited by - Stoffel on May 16, 2001 10:38:57 AM

This topic is closed to new replies.

Advertisement