Skip to main content
GameDev.net gamedev.net
🔒 Locked

STL vectors/maps and pointers

Started by OOProgrammer Apr 11, 2002 at 6:57 PM 3 replies 650+ views
Original Post
OOProgrammer
OOProgrammer
I''m trying to get an OO backbone for a game put together, and what I''m trying to do is have instances of a base class, Object, managed an inherited class, SystemObject. Ideally, the SystemObject class will use an STL map to store Objects and their vnum''s (integers) while individual Objects track their contents as a integer vector and their location as an integer. My problem is this: I''d like to refer to all Objects by pointers, but I''m getting a lot of complications because of the STL also tracking them as pointers. (That''s what it does, right?) So should I use a map of ? I tried this and got a lot of errors, and I see no good way to work with a map of Objects. The biggest problem (I think) is that the STL functions (like push_back()) are expecting references, not pointers. What can I do to reconcile my Object *''s with the STL? PS: I hope that made sense. ----------------------------------- "Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
-----------------------------------"Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
Stoffel
Stoffel
You can have both your key and your value in a map and have it work. You''ll have to write your own predicate, otherwise the default predicate will use if ptr a < ptr b (e.g. they''ll be ordered by their position in memory). You will have to be absolutely sure, however, that your key objects never change once they''re in the map, at least in any way that would affect how they are sorted. That''s a sure-fire way to get lost in your map and crash your program--it''s the reason that the key is the const part of the pair.
OOProgrammer
OOProgrammer
The pointer isn''t the key--the integer is. I''ll try again, hopefully with more success. And don''t worry, the key for a particular object will never change.

-----------------------------------
"Is the size of project directly proportional to the amount of stupidity to be demonstrated?"
-SabreMan
-----------------------------------"Is the size of project directly proportional to the amount of stupidity to be demonstrated?" -SabreMan
SabreMan
SabreMan
quote:
Original post by OOProgrammer
My problem is this: I'd like to refer to all Objects by pointers, but I'm getting a lot of complications because of the STL also tracking them as pointers. (That's what it does, right?)

STL containers actually have value semantics, which means that adding an object to a container will create a copy of that object and store the copy rather than the original. If you wish to simulate reference semantics, you can create a collection which contains pointers to the object you want to store.
quote:

So should I use a map of ? I tried this and got a lot of errors

That sounds kind-of right from what you are saying. As a general guideline, you should prefer to use smart pointers over bald pointers. Note that you must *never* use std::auto_ptr in an STL container, so you should download Boost and use boost::shared_ptr.
quote:

The biggest problem (I think) is that the STL functions (like push_back()) are expecting references, not pointers.

Well yes, but push_back() is not relevant to a map. Since map is a sorted collection, it doesn't make sense to add something to the back of the collection, you must use insert instead, which adds the item at the correct location. The other thing you should realise about insert is that you must insert a (key, value) pair<>, which you can create with the helper function make_pair(). Alternately, you can use the syntax "my_map[5] = new Object();".

Does that make any sense?

Edit: stuff.

[edited by - SabreMan on April 12, 2002 5:46:59 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.