Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving circular dependency in the State Pattern with C++
    text
    copied!<p>I'm trying to implement a State Pattern in C++, but have problems with the circular dependency. I have read other related material here - unfortunately it didn't help me. I don't have a lot of experience with C++, so bear with me. The following code is developed on a Ubuntu 10.10 machine in Eclipse Helios CDT:</p> <p><strong>ConcreteSystem.h</strong></p> <pre><code>#ifndef CONCRETESYSTEM_H_ #define CONCRETESYSTEM_H_ class SystemState; class ConcreteSystem { public: ConcreteSystem(); void SelfTestFailed(); void Restart(); private: friend class SystemState; SystemState *currentState; void ChangeState(SystemState *state); }; #endif /* CONCRETESYSTEM_H_ */ </code></pre> <p><strong>ConcreteSystem.cpp</strong></p> <pre><code>#include "ConcreteSystem.h" #include "SystemState.h" ConcreteSystem::ConcreteSystem() { currentState = SelfTest::GetInstance(); } void ConcreteSystem::SelfTestFailed() { currentState-&gt;SelfTestFailed(this); } void ConcreteSystem::Restart() { currentState-&gt;Restart(this); } void ConcreteSystem::ChangeState(SystemState *state){ currentState = state; } </code></pre> <p><strong>SystemState.h</strong></p> <pre><code>#ifndef SYSTEMSTATE_H_ #define SYSTEMSTATE_H_ class ConcreteSystem; class SystemState { public: virtual void Restart(ConcreteSystem *cs); virtual void SelfTestFailed(ConcreteSystem *cs); protected: virtual void ChangeState(ConcreteSystem *cs, SystemState *state); }; #endif /* SYSTEMSTATE_H_ */ </code></pre> <p><strong>SystemState.cpp</strong></p> <pre><code>#include "SystemState.h" #include "ConcreteSystem.h" void SystemState::Restart(ConcreteSystem *cs) { } void SystemState::SelfTestFailed(ConcreteSystem *cs) { } void SystemState::ChangeState(ConcreteSystem *cs, SystemState *state) { cs-&gt;ChangeState(state); } </code></pre> <p><strong>SelfTest.h</strong></p> <pre><code>#ifndef SELFTEST_H_ #define SELFTEST_H_ #include "SystemState.h" class SelfTest : public SystemState { public: SelfTest(); void SelfTestFailed(ConcreteSystem* cs); static SystemState* GetInstance(); private: static SystemState* instance; }; #endif /* SELFTEST_H_ */ </code></pre> <p><strong>SelfTest.cpp</strong></p> <pre><code>#include "SelfTest.h" #include "Failure.h" SystemState* SelfTest::instance = 0; SelfTest::SelfTest() { } void SelfTest::SelfTestFailed(ConcreteSystem *cs) { ChangeState(cs, Failure::GetInstance()); } SystemState* SelfTest::GetInstance() { if (instance == 0) { instance = new SelfTest(); } return instance; } </code></pre> <p><strong>Failure.h</strong></p> <pre><code>#ifndef FAILURE_H_ #define FAILURE_H_ #include "SystemState.h" class SelfTest; class Failure : public SystemState { public: Failure(); void Restart(ConcreteSystem* t); static SystemState* GetInstance(); private: static SystemState* instance; }; #endif /* FAILURE_H_ */ </code></pre> <p><strong>Failure.cpp</strong></p> <pre><code>#include "Failure.h" #include "SelfTest.h" SystemState* Failure::instance = 0; Failure::Failure() { } void Failure::Restart(ConcreteSystem* t) { ChangeState(t, SelfTest::GetInstance()); } SystemState* Failure::GetInstance() { if (instance == 0) { instance = new Failure(); } return instance; } </code></pre> <p>I have problem with the includes, which gives me some weird compiler errors. Anyone with a good solution to this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload