如何将原始指针的向量转换为唯一指针的向量?

Mar*_*kas 3 c++ smart-pointers raii raw-pointer

#include <vector>

enum ListOfGameStates
{
    // List of game states
};

class GameState()
{
    public:
        GameStates(); // Initializes protected (global) variables
        virtual ListOfGameStates run() = 0;
    protected:
        // Heavyweigh resource managers containing all resources and other global vars
}

class GameStateManager()
{
    public:
        GameStateManager();  // Creates all game states
        ~GameStateManager(); // Deletes all game states
        void run();          // Switches from one state to another state
    private:
        // A vector of raw pointers to game states. GameState is a base class.
        std::vector<GameState*> game_states_container;
}
Run Code Online (Sandbox Code Playgroud)

我想摆脱原始指针,这样我就不用担心异常和清理了.有一个简单的简单解决方案(我是一个非常愚蠢的青少年)或者它不值得吗?谢谢!

Chr*_*rew 8

只需将矢量更改为:

std::vector<std::unique_ptr<GameState>> game_states_container;
Run Code Online (Sandbox Code Playgroud)

delete在你的析构函数中删除任何东西.事实上,除非有其他工作要做,否则你可以完全摆脱析构函数.

unique_ptr是不可复制的,但它是可移动的,因此值得对C++ 11移动语义有所了解.当您想要添加unique_ptr到容器中时,您可以使用push_back提供传递临时值,例如函数的返回值:

game_states_container.push_back(createGameState());
game_states_container.push_back(std::make_unique<GameStateA>());  // C++14
Run Code Online (Sandbox Code Playgroud)

或者,如果您有一个局部unique_ptr变量,您可以使用std::move它将其移动到向量中:

std::unique_ptr<GameState> game_state = std::make_unique<GameStateA>();  // C++14
// auto game_state = std::unique_ptr<GameState>(new GameStateA);  // C++11
...
game_states_container.push_back(std::move(game_state));
Run Code Online (Sandbox Code Playgroud)

优良的做法unique_ptr是尽快将原始指针放入new(或最好使用std::make_unique).否则,如果在分配和包装之间抛出异常,unique_ptr则会导致内存泄漏.

它是无关的,unique_ptr但你的GameState应该有一个虚拟析构函数.

Live demo

  • 实际上,克里斯,你的答案现在有点误导:只要被推送的对象是临时的,`push_back`就可以在`unique_ptr`上正常工作,即使没有`std :: move`.`emplace_back`不是必需的,因为`unique_ptr`是不可复制的,这是必要的,因为没有从原始指针到`std :: unique_ptr`的隐式转换. (4认同)