Jump to content
  • Advertisement


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


MFC, CObList

This topic is 6398 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 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?

Share this post

Link to post
Share on other sites
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"

Share this post

Link to post
Share on other sites
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.h

class Enemy
virtual void doAI () = 0; // pure virtual


// in tank.h

class Tank : public Enemy
virtual void doAI () { /* do something */ }

// in main cpp file

CList<Enemy*, Enemy*> m_listEnemy; // list of pointers

m_listEnemy.AddTail (new Tank); // add a tank

Enemy *pEnemy = m_listEnemy.GetHead ();
pEnemy->doAI ();

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Guest Anonymous Poster

I use CObject for the serialization.

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

CWorld :: CDocument
CClan :: CObject
CWarrior :: CObject
CStockPile :: CObject

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..

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
Not sure why this post was resurrected from many moons ago, but..

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

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!