title: How to get instance of nested class in C++
body: <p>I have the following code:</p>
  
  <pre><code>class outer
  {
      struct inner
      {
          int var1;
          int var2;
          inner() { var1 = 1; var2 = 2; }
      };
  
      inner inner_instance;
  
  public:
      const inner *get_inner() { return &amp;inner_instance; }
  };
  
  int main(int argc, char *argv[])
  {
      // 1
      outer outer_instance;
      cout &lt;&lt; outer_instance.get_inner()-&gt;var1 &lt;&lt; endl;
  
      // 2
      // this cannot be compiled because outer::inner is private
      //const outer::inner *inner_ref = outer_instance.get_inner();
      //cout &lt;&lt; inner_ref-&gt;var1;
  
      // 3
      const int *inner_var2 = (int *) outer_instance.get_inner();
      inner_var2++;
      cout &lt;&lt; *inner_var2 &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>I understand why No.2 cannot be compiled. I just do not know what is the design idea of the compiler that allows access to the public fields of a private nested class but not the nested class itself, like No.1. The instance data is still in the memory. If I know the structure of such private nested class, I can still achieve assigning like No.3 does.</p>
  
  <p>Does that mean it is better not to return pointer or reference of a private nested class in a public function? And if I really have to return something about the nested class to the outer world, make it public?</p>
  
  <p>Thanks!</p>