我正在 C++ 中实现状态模式,通常一切正常并且根据需要工作,但是在状态多次更改后我出现分段错误,堆栈分配不断上升直到达到最大值,然后崩溃雇用代码: 接口:
class ControllerT1; // Forward declaration
class IControllerStateT1 {
public:
virtual ~IControllerStateT1() {}
virtual void handle(ControllerT1* controller) = 0;
};
class ControllerT1 {
public:
ControllerT1(std::vector<int> &input, std::vector<int> &output);
~ControllerT1();
void stop();
void initialize();
void run();
void setState(IControllerStateT1* newState);
private:
IControllerStateT1* currentState;
};
#include "ControllerT1.h"
#include "StopT1.h"
ControllerT1::ControllerT1(std::vector<int> &input, std::vector<int> &output) : currentState(new StopT1()) {
// ControllerInputs = input;
// ControllerOutputs = output;
}
ControllerT1::~ControllerT1() {
delete currentState;
}
void ControllerT1::run() {
currentState->handle(this);
}
void ControllerT1::stop() {
currentState->handle(this); …Run Code Online (Sandbox Code Playgroud)