Resolving a Forward Declaration Issue Involving a State Machine in C++ -


I recently returned to C ++ development after a break, and about the implementation of the state design pattern Have a question. I am using Wahali Pattern, according to Goff's book.

My problem is that the state machine is based on the hardware used only as part of some embedded system - hence the design is fixed and can not be changed, it can not be changed to two states (special There is a circular dependency between), and I am trying to solve it. There is a simplified code here (note that I tried to solve it using the headers in general but still had problems - I have left them in this code snippet):

  #include & lt; Iostream & gt; #include & lt; Memory & gt; using namespace std; Reference to class {public: friend class state; Context () {} private: state * m_state; }; Class state {public: state () {} virtual zero trigger1 () = 0; Virtual Zero Trigger 2 () = 0; }; Class LLT: Public State {Public: LLT ()}} Zero Trigger 1 () {New DH (); } Zero trigger 2 () {new DL (); }}; Class all: public state {public: ALL () {} zero trigger 1 () {newlt (); } Zero trigger 2 () {new DH (); }}; // DL should know about DH Square DL: public state {public: DL () {} zero trigger 1 () {new ALL (); } Zero trigger 2 () {new DH (); }}; Class HLT: Public State {Public: HLT () {} Zero Trigger 1 () {New DH (); } Zero trigger 2 () {new DL (); }}; Class AHL: Public State {Public: AHL () {} Zero Trigger 1 () {New DH (); } Zero trigger 2 () {new HLT; }}; // DH needs 'address' about DL class DH: public state {public: DH ()}} zero trigger 1 () {new AHL (); } Zero trigger 2 () {new DL (); }}; Int main () {auto_ptr & lt; LLT & gt; Llt (new LLT); Auto_ptr & lt; All & gt; All (new ALL); Auto_ptr & lt; DL & gt; DL (new DL); Auto_ptr & lt; HLT & gt; HLT (new HLT); Auto_ptr & lt; AHL & gt; Ah (new AHL); Auto_ptr & lt; DH & gt; DH (new DH); Return 0; }  

The problem is basically that in the state pattern, state changes are made by implementing the changestant method in the reference category, which invites the constructor of the next state.

Due to circular dependence, I can not call the constructor because it is not possible to pre-define both the consultants of 'problem' states.

I have taken a look at the article, and the template method seems to be the ideal solution - but it does not compile and knowledge of my template is not limited ...

The idea is that the helper class is a reference to the subclass states, through many heritage, to see if it is possible to specify the constructor of the base class and the constructor of state subclasses. But I think it was relatively ambitious ...

In the end, would the best way to solve all the problem directly related to the factory method design pattern?

zero trigger 2 (); }; Inline Zero DL :: Trigger 2 () {New DH (); }

Define member classes that rely on the subsequent class definitions then those classes are defined inline The keyword is required only if you define the class members function outside in the header file.

As a side, why are you just using new DH () in your work; You are leaking memory everywhere!


Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -