title: Why does the auto keyword allow creating instances of privately declared inner-classes or inner-structs?
body: <p>I found something weird in C++ that I don't understand why it's happening.</p>
  
  <p>I have an inner class with a private struct definition, it looks like this:</p>
  
  <pre><code>#include &lt;string&gt;
  class A {
  private:
      class B {
      private:
          struct C {
              int lol;
              std::string ok;
          };
      public:
          B() {}
          C* makething();
      };
  public:
      A() {}
      void dostuff();
  };
  </code></pre>
  
  <p><code>C* makething();</code> simply returns a new struct C, like this:</p>
  
  <pre><code>A::B::C* A::B::makething()
  {
      C* x = new C;
      return x;
  } 
  </code></pre>
  
  <p>So now if the implementation for <code>void dostuff();</code> is like this:</p>
  
  <pre><code>void A::dostuff()
  {
      B x = B();
      B::C* thing = x.makething(); // compile error
      thing-&gt;lol = 42;
      thing-&gt;ok = "some words";
      std::cout &lt;&lt; thing-&gt;lol &lt;&lt; " " &lt;&lt; thing-&gt;ok;
  }
  </code></pre>
  
  <p>It gives me the error: <code>C2248 'A::B::C': cannot access private struct declared in class 'A::B'</code> This was expected, because struct C is declared as private within class B. </p>
  
  <p>However, if I change the line <code>B::C* thing = x.makething();</code> to <code>auto thing = x.makething();</code>, it will compile and no longer gives me the error that struct C is private, and then I can modify the struct's values like I did in the <code>makething()</code> function.</p>
  
  <p>Why is this happening? Why does the <code>auto</code> keyword allow me to create instances of privately declared structs or classes?</p>
  
  <p>Edit: I am using Visual Studio Community 2015 as the compiler.</p>
  
  <p>Edit 2: Didn't realize this was a duplicate, couldn't find anything when I was searching for an answer. Thanks for the responses though everyone, it makes sense now.</p>