Archived

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

eotvos

STL map question

Recommended Posts

eotvos    127
I'm just going to cut and paste some code out of the project I'm working on to illustrate my question... I have a class called SPRITE that has a constructor with arguments: SPRITE(CL_ResourceManager* resources, char* dir, int num_states) Next, I wanted to make a map of SPRITEs. My first idea was to do something like this: SPRITE* bigrock; bigrock = new SPRITE(&rock_resource, bigrock_dir, 2);
  
map<int,bigrock> b_rocks;
  
This didn't work So, the question is: How do I initialize the map so that I can set the arguments of the class constructor? Edited by - eotvos on November 9, 2001 7:55:38 PM

Share this post


Link to post
Share on other sites
Hedd    122
If you want to declare a map of sprites then you need keys and sprites. Of course, the key doesn''t have the be an int I like I have here. It can be a string or anything.

map< int, SPRITE * > b_rocks;

SPRITE* bigrock;
bigrock = new SPRITE(&rock_resource, bigrock_dir, 2);
b_rocks[1] = bigrock;

// and retrieve it like this
SPRITE * rock = b_rocks[1];

Of course, you will need to delete all those pointers you put into the map before destroying the map itself or you will have a big fat memory leak waiting for you. I suggest you use a Smart Pointer. There is one over at www.boost.org. Don''t use auto_ptr. It is not for these purpose.
Conrad

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
to use a map you need a constructor with no arguments. So just add a constructor without any arguments in addition to the one you already have. Or if you don''t want to do that make your map hold pointers. If you don''t want to do that either, well then you can''t use a map.

Share this post


Link to post
Share on other sites