访问存储在基类

时间:2017-09-26 04:46:13

标签: c++ pointers base-class

我有一个名为StateMachine的类,它控制所有可能的实体状态。

然后我有一个名为State的类,它是唯一Entity类的基类,例如Attack State,Flee State。

当创建一个新的唯一状态(在StateMachine中)时,它传入一个StateMachine指针,然后该指针将存储在State基类中,以便创建的每个唯一状态都可以访问其状态机。

当我尝试访问指针成员(使用 - >运算符)时,它很简单,没有提出任何公共方法,我也不知道为什么。

如果有人有任何线索,我们将不胜感激。

StateMachine.h

using STATE_PTR = std::shared_ptr<State>;

// Class to implement a finite state machine using the state desing pattern

    class StateMachine
    {
    public:
    StateMachine();
    ~StateMachine();

    void OnEnter(STATE_NAME sn);
    void OnExit();
    void OnEvent(STATE_SYMBOL & ss);
    void OnTick(float st);
    void ChangeState(STATE_NAME const & sn);
    void RegisterState(ENTITY_CLASS const & ec);

    typedef std::map<STATE_NAME, STATE_PTR>     STATE_REGISTRY;

private:
    STATE_REGISTRY  state_registry;
    STATE_NAME      current_state;
};

StateMachine.cpp

using namespace::std;


StateMachine::StateMachine()
    : state_registry()
{
    current_state = STATE_NAME::UNKNOWN;
}

StateMachine::~StateMachine()
{
    state_registry.clear();
}

void StateMachine::OnEnter(STATE_NAME sn)
{
    current_state = sn;

    if (state_registry[current_state] != nullptr)
    {
        state_registry[current_state]->OnEnter();
    }
}

void StateMachine::OnExit()
{
    if (state_registry[current_state] != nullptr)
    {
        state_registry[current_state]->OnExit();
    }
}

void StateMachine::OnTick(float st)
{

}

void StateMachine::OnEvent(STATE_SYMBOL & ss)
{
    state_registry[current_state]->OnEvent(ss);
}

void StateMachine::RegisterState(ENTITY_CLASS const & ec)
{
    switch (ec)
    {
    case ENTITY_CLASS::PLAYER_TANK :
        state_registry.insert(std::make_pair(STATE_NAME::STATE_1, std::make_shared<PlayerTankState1>(this)));
        state_registry.insert(std::make_pair(STATE_NAME::STATE_2, std::make_shared<PlayerTankState2>(this)));
        break;
    case ENTITY_CLASS::ENEMY_TANK :
        state_registry.insert(std::make_pair(STATE_NAME::STATE_3, std::make_shared<EnemyTankState1>(this)));
        state_registry.insert(std::make_pair(STATE_NAME::STATE_4, std::make_shared<EnemyTankState2>(this)));
        state_registry.insert(std::make_pair(STATE_NAME::STATE_5, std::make_shared<EnemyTankState3>(this)));
        break;
    default:
        break;
    }
}

void StateMachine::ChangeState(STATE_NAME const & sn)
{
    state_registry[current_state]->OnExit();
    current_state = sn;
    state_registry[current_state]->OnEnter();
}

State.h

class StateMachine;         // Forward decloration of the StateMachine class

// Base class for all states of the game system
class State
{
protected:
    State(StateMachine * p)
        : mp_Owner(p)
    {}

public:
    virtual ~State() {}

    virtual void OnEnter() = 0;
    virtual void OnExit() = 0;
    virtual void OnTick(float) = 0;
    virtual void OnEvent(STATE_SYMBOL) = 0;

    StateMachine * mp_Owner;
};

EnemyTankState.cpp(独特状态)

    EnemyTankState1::EnemyTankState1(StateMachine * p)
    : State(p)
{
}

EnemyTankState1::~EnemyTankState1()
{
}

void EnemyTankState1::OnEnter()
{
    cout << "Hi From Enemy Tank: Partolling State" << endl;
}

void EnemyTankState1::OnExit()
{
    cout << "Bye From Enemy Enemy Tank: Partolling State" << endl;
}

void EnemyTankState1::OnTick(float dt)
{

}

void EnemyTankState1::OnEvent(STATE_SYMBOL ss)
{

    switch (ss)
    {
    // Takes Enemy Tank to Attacking State
    case STATE_SYMBOL::SYMBOL_2 :
        mp_Owner->
        break;
    }
}

在上面的代码示例中,行mp_Owner->让我感到悲伤,因为它没有像使用类指针那样打开公共方法列表。

非常感谢任何帮助。很抱歉代码很长,我无法想出任何其他解决问题的方法。

0 个答案:

没有答案