使用RAII填充成员对象图并使用智能指针存储一个成员的正确方法

问题描述

因此,我对C ++还是陌生的,正在尝试了解智能指针和RAII设计模式。我的问题是:说我有一个包含对象映射的对象。我希望一次激活一个对象,即希望有一个指针指向地图中的一个对象。使用智能指针进行RAII处理的正确方法是什么?下面是我到目前为止尝试过的。

//StateMachine.h
    std::unique_ptr<GameObject> p1Paddle = std::make_unique<GameObject>(Paddle());
    std::unique_ptr<GameObject> p2Paddle = std::make_unique<GameObject>(Paddle());
    std::unique_ptr<GameObject> ball = std::make_unique<GameObject>(Ball());
//StateMachine.cpp
StateMachine::StateMachine()
{
    gameObjects["p1Paddle"] = std::pair <bool,std::unique_ptr<GameObject>>(false,std::move(p1Paddle));
    gameObjects["p2Paddle"] = std::pair <bool,std::move(p2Paddle));
    gameObjects["ball"] = std::pair <bool,std::move(ball));
}

void StateMachine::ChangeState(std::string key)
{
    activeObject = std::move(gameObjects[key]);
}

解决方法

不是使用mapsmart-pointer,而是根据您的要求(在给定的时间只需要一种对象类型),而可能希望使用基于priority queue的实现。 ..

这是一个伪实现:

#include <algorithm>
#include <cstdint>
#include <queue>
#include <string>
#include <vector>

enum class ComponentType {
    PADDLE,BALL
};

class Component {
protected:
    ComponentType type_;
    uint32_t id_;
    priority_;

    Component(ComponentType type,uint32_t id,float priority) 
     : type_{type},id_{id},priority_{prioity};
public:
    virtual ~Component() {}
    auto type() const { return type_; }
    auto id() const { return id_; }
    auto priority() const { return priority_; }
    
    void updatePriority(float newPriority) { priority_ = newPriority; }         
};

class Paddle : public Component {
private:
    std::string name_;
public:
    Paddle(std::string_view name,float priority) :
      Component(ComponetType::PADDLE,std::stoi(name.data()),priority),name_{name} 
    {}

    auto name() const { return name_; }
};

class Ball : public Component {
private:
    std::string name_;
public:
    Ball(std::string_view name,float priority) :
      Component(ComponentType::BALL,name_{name}
    {}

    auto name() const { return name_; }
};

class CmpFunc {
public:
    int operator(){ const Component& a,const Component& b) {
        return a.priority() > b.priority();
    }
};

class Game {
private:
    //std::vector<Component*> components_;
    std::priority_queue<Component*,std::vector<Component*>,CmpFunc> priority_;
public:
    void initialize() {
        Paddle paddle1("paddle_1",0.1f);
        Paddle paddle2("paddle_2",0.2f);
        Ball ball("ball",0.3f");
        addComponent(&paddle1);
        addComponent(&paddle2);
        addComponent(&ball);
    }

    void addComponent(Component* component) {
        if (component == nullptr)
           throw std::runtime_exception( "invalid component pointer!" );

        //components_.push_back(component);
        priority_.push(component);            
    }

    auto getComponent() {
        if (!priority_.empty()) {
            auto component = priority_.top();
            priority_.pop();
            /*components_.erase(
              std::remove(components_.begin(),components_.end(),component),components_.end()
            );*/
            return component;
        }
        return nullptr;
    }
};

这只是显示优先级队列的伪示例代码...我没有显示任何更新或更改对象优先级队列的实现,也没有显示如何基于某些{ {1}}或state X ...这对您来说是一种练习...

我无法确定此代码是否可以编译和运行,因为我是在这里输入它的,因此尚未finite-state-machine代码对其进行测试。您可以尝试使用它,然后尝试对其进行编译,然后从那里进行扩展以满足您自己的需求...

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...