Instantiating a new object. Not sure if this is correct?
<p>I have a member pointer with this type:</p>

<pre><code>const TopState&lt;TestHSM&gt;* state_;
</code></pre>

<p><code>state_</code> is of polymorphic type.</p>

<p><code>TopState</code> is the base:</p>

<pre><code>template&lt;typename H&gt;
struct TopState {
//... functions etc
};
</code></pre>

<p>There is a bit of a hierarchy and finally <code>LeafState</code> which is not abstract:</p>

<pre><code>template&lt;typename H, unsigned id,
typename B=CompState&lt;H,0,TopState&lt;H&gt; &gt; &gt;
struct LeafState : B {
   //...functions
   static const LeafState obj;
};
</code></pre>

<p>The following objects represent state:</p>

<pre><code>//indentation to indicate state nesting
typedef CompState&lt;TestHSM,0&gt;      Top;
typedef CompState&lt;TestHSM,1,Top&gt;    S0;
typedef CompState&lt;TestHSM,2,S0&gt;       S1;
typedef LeafState&lt;TestHSM,3,S1&gt;         S11;
typedef CompState&lt;TestHSM,4,S0&gt;       S2;
typedef CompState&lt;TestHSM,5,S2&gt;         S21;
typedef LeafState&lt;TestHSM,6,S21&gt;          S211;
</code></pre>

<p>Note only <code>S11</code> and <code>S211</code> (<code>LeafState</code>'s) can be instantiated.</p>

<p>I have a <code>TestHSM</code> class which looks like this:</p>

<pre><code>class TestHSM {
public:
TestHSM()  {
    state_ = new S11;
}

//fix destruction - problem
~TestHSM() {
    //reset to s11
//  state_ = &amp;S11;
//  delete state_;
//  state_ = 0;
}

void next(const TopState&lt;TestHSM&gt;&amp; state)
{ 
    state_ = &amp;state; 
}

private:
const TopState&lt;TestHSM&gt;* state_;
};
</code></pre>

<p>My problem right now is creation of the <code>state_</code> object.  See constructor above.  This works but I am not entirely sure if it is correct way to do things?</p>

<p><code>S11</code> is the first state where the object can be instantiated - and in my program <code>S11</code> 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?</p>

<p>In addition, if I do this then I get heap memory runtime errors when I attempt to delete <code>state_</code> - see commented out destructor code.</p>

<p>Any suggestions?</p>

<p>Angus </p>
