Can a C++ map contain a pointer to my arbitrary class? -


I am writing a small game engine as a summer project, and I'm struggling with STL Maps .

I have declared a class RenderList to capture the object RenderList will be sent to a renderer class to work.

RenderList in a map

All this works, as long as I try to get a Entity * from the map and I get it:

Vc / include / xtree expression: map / set iterator is not dereferencable.

This code is available to get the pointer.

  entity * RenderList :: getByName (std: string str) {return objects.find (str) - & gt; second; }   

I need to capture an indicator, not a real object, because there are different sub-classes of entity which will be required.

I am quite new to STL, should I not store points in the map?

Surely I should be allowed to do this, or is there a better idea to store things instead?

And finally, am I just doing it wrong ?!

Hope this question is not a duplicate, I searched it early in the day. Even if it is better in the GamingWatch, I will post it there.

if key is not found then map :: search (key) Returns the "past-end" iterator, i.e. the value returned by map :: end () , and that iterater does not point to any element so it can not be dereferenced.

Are you sure they are in the key map?

If you would like to do something like a return tap, if the key is not found, then you can compare the returned iterator to end for example

  Entity * RenderList :: getByName (std :: string str) {map_type :: iterator = objects.find (str); If (this == objects.and ()) returns zero; It back-> second; }   

Where RenderList defines typedef:

  typedef map & lt; Std :: string, entity * & gt; map type;   

(NB defines the FF typed for the containers used by the always my classes as a description, since it Map_type :: iterator to map gt; :: iterator and because if I change the container something else, then use it For all the code must be changed such as map gt; :: Iterator I only map_type :: iterator Can / may leave the area as me and it still works perfectly.)

About the general design, what you instead of a raw pointer boost :: shared_ptr & lt; Unit & gt; or std :: tr1 :: shared_ptr & lt; Unit & gt; can store? It will be more secure and easier to manage object lifetime.

Comments