Jump to content
  • Advertisement


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


From MFC to STL

This topic is 5528 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 programming a RPG and I'm updating my data handling My major effort had used the MFC collections - mainly CPtrList and used a Base class that I operated on
class BaseObj
	~BaseObj() {};
	char KeyName[40];
	int  KeyCode;
	char Key2ndName[40];
	int  KeySourceCode;
	int  UPriority;
	int  ObjType;
	int  nID;

// And then derived things like 

class InventoryItem : public BaseObj
	int		ItemQuantity;
	double	ItemPrice;
	double	ItemLbs_type;
	double	ItemOunces; 

// and 

class Actor : public totplist
	char  Home[20];
	short careerFlag;
    	char  family_occupation[20];
	short convocation;
    	short gender, species, bdate[2], sunsign[2];
	short basicstats[14];
	CPtrList *pInventoryList;
	CPtrList *pWeaponList;

// And used functions like:

SortByKeySourceCode(CPtrList *pTopList)
SortByKeyCode(CPtrList *pTopList)
SortByUPriority(CPtrList *pTopList)
SortByUPriorityReverse(CPtrList *pTopList)
SortByKeyName(CPtrList *pTopList)
SortByKey2ndName(CPtrList *pTopList)
Now I want to move all this into STL Question 1) Pretty basic but how do I address a list across different cpp files? in PCEdit.cpp I would have std::list WeaponList; How would I access it in another file? Would I use a reference in an .h file? Question 2) If I have class Actor { std::list WeaponList; std::list SpellList; std::list ItemList; short gender, species, bdate[2], sunsign[2]; short basicstats[14]; int numWeapons, numSpells, numItems; }; Actor MyGuy; I can't just fwrite(File_Pointer, MyGuy, 1); Are there any special function that allow you to save lists to file? ZoomBoy Developing a iso-tile 2D RPG with skills, weapons, and adventure. See my old Hex-Tile RPG GAME, character editor, diary, 3D Art resources at Check out my web-site [/source] [edited by - ZoomBoy on May 28, 2003 11:53:52 PM]

Share this post

Link to post
Share on other sites
You have to tell the list what it's going to hold.
You access the variable you create from a template just like any other, except you want to make a typedef for the list so you can find the iterator type later.

You'll want to write a function that take a moniker and creates an object from it - either an ID or a name.

typedef std::list<InventoryItem*> InventoryItemList;
InventoryItemList list_of_stuff;

struct BaseObj
//Most base classes should have a virtual dtor

virtual ~BaseObj(){}

struct InventoryItem : BaseObj
virtual ~InventoryItem(){}
virtual ItemID GetID()=0;
virtual WriteToDisk(FILE*)=0;
virtual ReadFromDisk(FILE*)=0;

enum ItemID

InventoryItem* CreateItem(ItemID item_id)
//This isn't the best way, but will get you started

if(item_id == SwordID)
return new Sword;


//Write a range of items out to disk

template<class FI>
void WriteItems(FILE* hfile, FI begin, FI end, size_t n)
//Size first, always first

fwrite(hfile, n, sizeof(size_t));
FI it = begin;
for(; it!=end; ++it)
InventoryItem* item = *it;
ItemID id = item->GetID();
fwrite(&id, sizeof(ItemID), 1, hfile);
//Use like this:

FILE* save;

WriteItems(save, WeaponList.begin(), WeaponList.end(), WeaponList.size());

//II means insertion iterator

template<class II>
void ReadItems(FILE* hfile, II ii)
size_t n;
fread(&n, sizeof(size_t), 1, hfile);
for(size_t i=0; i<n; ++i)
ItemID id;
fread(&id, sizeof(ItemID), 1, hfile);
InventoryItem* item = CreateItem(id);
*ii = item;
FILE* restore;

ReadItems(restore, std::back_inserter(WeaponList));

This is a mish-mash of template and OO code... good luck

[edited by - Magmai Kai Holmlor on May 28, 2003 11:50:19 PM]

Share this post

Link to post
Share on other sites
Ooops, I had to re-redit my response
Most of my questions ended in the source section
so it''s not as clear as I''d like.

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!