Auto_ptr and containers - C ++

I am currently working on a two-dimensional game engine, and I read about auto_ptr and how you should never put them in standard containers.

My engine has this structure:

StateManager - has many → states.

States are created and distributed mainly outside the engine. I want the engine to keep a list / vector of all states, so I can switch between them on command.

For instance:


SomeState *test = new SomeState();
StateManager->registerState(test);

Since states die if and only if 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); }

StateManager , . , activeState, , stateList.

?

+3
5

std::vector <State> ? , , .

, Boost Pointer Container .

+9

StateManager , State *, . auto_ptr , StateManager.

, , , . auto_ptr , . shared_ptr .

+4

( ..), , std::vector<State>. activeState ( ).

+3

auto_ptr , , .

std::auto_ptr<SomeState> test(new SomeState());
StateManager->registerState(test.get());

"test" , , SomeState, StateManager. , StateManager , , , .

, . boost:: shared_ptr.

+1

auto_ptr s .

void foo() {  MyObject * pNewObject = new MyObejct;

// -   pNewObject; }

, - //Do Something, d, . Auto_ptrs .

( ). auto_ptr delete ptr; delete [] ptr; auto_ptr , void foo() {

unsigned char * pStream = new unsigned char []; ....

[] pStream; }

If you do not have a large number of states, and it is not so greedy for memory, I recommend using Singleton objects for your state classes. Therefore, you do not need to worry about memory management. The state manager can handle the active state and state objects, but I do not think it is a good idea to initialize the state object outside the state manager, and the rest is in the statemanager and elsewhere. make sure creation and destruction are unified

0
source

Source: https://habr.com/ru/post/1754394/


All Articles