I'm currently working on a 2D game engine and I've read about auto_ptr's and how you should never put them in standard containers.
My engine has this structure:
StateManager -- has many --> State's.
States are created and allocated in main, outside of the engine. I want the engine to store a list/vector of all the states so I can change between them on command.
For example:
SomeState *test = new SomeState();
StateManager->registerState(test);
Since states die when and only when the application dies, can I use this approach?
std::auto_ptr<SomeState> test(new SomeState());
StateManager->registerState(test.get());
// Inside StateManager State *activeState; // State manager then maintains a vector std::vector<State*> stateList; // and upon registerState it adds the pointer to the vector void registerState(State *state) { stateList.push_back(test); }
The StateManager should only maintain a vector of pointers to the states, it shouldn't need to take ownership. When I want to change state in the engine, I just change the activeState pointer to point to the desired state which is found in the stateList vector.
Is this a bad approach?
Why not use std::vector<State> instead and not worry about storing pointers? It sounds like the list of states is fixed so any iterators will not be invalidated during the lifetime of the program.
Alternatively, I've found the Boost Pointer Container library to be useful for this sort of thing.