title: Returning a const pointer to a const data member and the 'auto' keyword. A bit confused
body: <p>I've recently been learning C++ and just today have been introduced to <em>const</em> and the concept of const correctness. In an attempt to better understand the theory, I've been writing a series of simple programs to make sure that I understand the concept correctly. I thought I understood everything, but then when using the <em>auto</em> keyword in one of the programs, I seem to have got a bit stuck.</p>
  
  <p>In order to test that I understood how const pointers work I wrote a simple program. I won't bother posting the whole thing since there's only two parts of it that are relevant. I have a class with a const data member of type int:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>const int tryToChangeMe;
  </code></pre>
  
  <p>Within this class I also have a member function that returns a const pointer to the above const int:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>const int* const MyClass::test()
  {
      return &amp;tryToChangeMe;
  }
  </code></pre>
  
  <p>In my main function I then call the above function, making use of the <em>auto</em> keyword. In order to test that what I think I know about <em>const</em> is correct I then attempt to reassign the <em>tryToChangeMe</em> variable through the pointer. Like so:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>auto temp = myClass.test();
  *temp = 100;
  </code></pre>
  
  <p>As I expected, the program wouldn't compile due to the error that I caused when trying to assign a value to a <em>const</em> variable. However, I didn't just return a pointer to <em>const</em>, I returned a <em>const</em> pointer to a <em>const</em> (at least that's what I thought I did). So to test this, I attempted to reassign the pointer to a new memory address, quite confident that I'd get a similar compilation error:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>temp = new int;
  </code></pre>
  
  <p>But quite confusingly the program compiled without any issue. Stepping through with the debugger revealed that sure enough, the pointer was losing its original address and being assigned a brand new one. Wondering what was going on, I just happened by chance to remove the <em>auto</em> keyword and replace it with the variable's full type:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>const int* const temp = myClass.test();
  </code></pre>
  
  <p>Upon testing everything again, the results were as expected and this time I was not able to reassign the pointer to a new address.</p>
  
  <p>So after all that I guess my question is, why? Why does the <em>auto</em> keyword allow you to bypass the <em>const</em> qualifier of pointers? Did I do something wrong?</p>
  
  <p>By the way, I'm not sure if it matters but I'm using Visual Studio 2015 preview</p>