STL map question

Started by
2 comments, last by eotvos 22 years, 5 months ago
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
Advertisement
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
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.
BTW If you use the boost smart pointer just realise that it isn''t thread-safe. [unless they have changed it since I was there last]

Dire Wolf
www.digitalfiends.com
[email=direwolf@digitalfiends.com]Dire Wolf[/email]
www.digitalfiends.com

This topic is closed to new replies.

Advertisement