I have a member pointer with this type:
const TopState<TestHSM>* state_;
state_
is of polymorphic type.
TopState
is the base:
template<typename H>
struct TopState {
//... functions etc
};
There is a bit of a hierarchy and finally LeafState
which is not abstract:
template<typename H, unsigned id,
typename B=CompState<H,0,TopState<H> > >
struct LeafState : B {
//...functions
static const LeafState obj;
};
The following objects represent state:
//indentation to indicate state nesting
typedef CompState<TestHSM,0> Top;
typedef CompState<TestHSM,1,Top> S0;
typedef CompState<TestHSM,2,S0> S1;
typedef LeafState<TestHSM,3,S1> S11;
typedef CompState<TestHSM,4,S0> S2;
typedef CompState<TestHSM,5,S2> S21;
typedef LeafState<TestHSM,6,S21> S211;
Note only S11
and S211
(LeafState
's) can be instantiated.
I have a TestHSM
class which looks like this:
class TestHSM {
public:
TestHSM() {
state_ = new S11;
}
//fix destruction - problem
~TestHSM() {
//reset to s11
// state_ = &S11;
// delete state_;
// state_ = 0;
}
void next(const TopState<TestHSM>& state)
{
state_ = &state;
}
private:
const TopState<TestHSM>* state_;
};
My problem right now is creation of the state_
object. See constructor above. This works but I am not entirely sure if it is correct way to do things?
S11
is the first state where the object can be instantiated - and in my program S11
is the first state at startup. The code 'works' as expected. Or seems to anyway. But I am not sure that this is the optimal or even correct way to instatiate the fist state?
In addition, if I do this then I get heap memory runtime errors when I attempt to delete state_
- see commented out destructor code.
Any suggestions?
Angus