title: How do you access variables that are allocated on the heap in C++ even after leaving the scope?
body: <p>I'm having issues understanding the new keyword in C++. I understand that in order to not have memory popped off from stack after leaving scope; you can store it on heap. However in this example I get an error saying "identifier p is undefined" </p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  class Person {
  public: 
      int age; 
      std::string name;
  
      Person() {
  
      }
  
      Person(int a, std::string n): age(a), name(n){}
  };
  
  
  int main() {
      {
          Person* p = new Person(5, "bob");
      }
  
      std::cout &lt;&lt; (*p).age; 
  
  }
  </code></pre>
  
  <p>As you can see in my main function I've created another scope where I create a Person object on heap, from what I know the object will still exist after leaving the curly brackets, but then why is it not recognizing object p? </p>
  
  <p>I've tried derefrencing p but the error remains the same. 
  Any help would be great! 
  Thanks </p>