[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStateMultiFile
In its header file, the State has a forward declarion to its Context. Therefore, in its implementation file (State.cpp), the header file of the Context needs to be #included.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStateMultiFile
(C++) State in multiple files
The files in this example are:- main.cpp, containing the program itself
- Enum.h, contains the possible States
- Context.h
- Context.cpp
- State.h
- State.cpp
In its header file, the State has a forward declarion to its Context. Therefore, in its implementation file (State.cpp), the header file of the Context needs to be #included.
main.cpp
int main() { const std::auto_ptr<Context> myContext(new Context); myContext->setState(plus); myContext->doIt(); myContext->setState(minus); myContext->doIt(); }
- include <memory>
- include "Context.h"
Enum.h
enum enumState{ plus, minus}; //End of: #ifndef __ENUM_H
- ifndef __ENUM_H
- define __ENUM_H
- endif
Context.h
- ifndef __CONTEXT_H
- define __CONTEXT_H
class Context { friend class StateBase; friend class StatePlus; friend class StateMinus; public: Context(); void setState(const enumState&); void doIt() const; private: std::auto_ptr<StateBase> mState; const int mValue1; const int mValue2; }; //End of: #ifndef __CONTEXT_H
- include <iostream>
- include <memory>
- include <cassert>
- include "State.h"
- include "Enum.h"
- endif
Context.cpp
//--------------------------------------------------------------------------- Context::Context() : mState(new StatePlus(this)), mValue1(std::rand()%100), mValue2(std::rand()%100) { //Nothing } //--------------------------------------------------------------------------- void Context::setState(const enumState& state) { switch(state) { case plus: mState.reset(new StatePlus(this)); break; case minus: mState.reset(new StateMinus(this)); break; default: assert(!"Unknown state"); std::exit(1); } } //--------------------------------------------------------------------------- void Context::doIt() const { //The function doIt is delegated to the State std::cout << "Doing it with " << mValue1 << " and " << mValue2 << ": " << mState->doIt() << std::endl; } //---------------------------------------------------------------------------
- include "Context.h"
State.h
class Context; //Forward declaration of Context class StateBase { public: StateBase() { mContext = 0; } virtual int doIt() const = 0; protected: Context * mContext; }; class StatePlus : public StateBase { public: StatePlus(Context *); int doIt() const; }; class StateMinus : public StateBase { public: StateMinus(Context *); int doIt() const; }; //End of: #ifndef __STATE_H
- ifndef __STATE_H
- define __STATE_H
- endif
State.cpp
- include "State.h"
//--------------------------------------------------------------------------- StatePlus::StatePlus(Context * context) { mContext = context; } //--------------------------------------------------------------------------- StateMinus::StateMinus(Context * context) { mContext = context; } //--------------------------------------------------------------------------- int StatePlus::doIt() const { return (mContext->mValue1 + mContext->mValue2); } //--------------------------------------------------------------------------- int StateMinus::doIt() const { return (mContext->mValue1 - mContext->mValue2); } //---------------------------------------------------------------------------
- include "Context.h"
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
