title: Inner class accessing outer class
body: <p>I am relatively new to C++, and I have looked a lot for an answer for this thing but I never got a satisfying answer.</p>
  
  <p>Let's say I have a structure called <code>FSM</code>. Eventually in my code, multiple instances of <code>FSM</code> can be created. One of <code>FSM</code>'s attributes is <code>int X</code> which is not static, every instance of <code>FSM</code> should have its own value for <code>X</code>.</p>
  
  <p>Now, one of <code>FSM</code>'s attributes is another structure <code>submachine</code> which needs to read the value of <code>X</code> like this:</p>
  
  <pre><code>struct FSM
  {
    public:
      int x;
  
      int getX(){return x;}
  
      struct submachine
      {
          void onentry() {int g = getX();};
      };
  };
  </code></pre>
  
  <p>This gives the following error:</p>
  
  <blockquote>
    <p>Error: 'FSM::getX' : illegal call of non-static member function</p>
  </blockquote>
  
  <p>My question is, <code>submachine</code> is a member of <code>FSM</code>, so shouldn't it have access to local instances of all the attributes of <code>FSM</code>?  And if not, when we create an instance of <code>FSM</code>, wouldn't we be creating an instance of all its members i.e. <code>submachine</code>?  And if so, then why do we need to create an object that <code>onentry()</code> needs?</p>
  
  <p>I am assuming the compiler is correct, so I would also want to know if there's a way to make this work.</p>
  
  <p>NOTE: Unfortunately, the instances of the inner structures (<code>submachine</code>) are instantiated when an event is called and thus I can only define the type, and not instantiate objects for them in <code>FSM</code>.</p>