#include <iostream>
using namespace std;
class A
{
private:
   int m_i;
   friend int main(int argc, char const *argv[]);
public:
   A (int i = 0):m_i(i){};
   void display()
   {
       cout << m_i << endl;
   }
   int result() {return m_i;}
};
void createA(A *pa)
{
   pa = new A(1);
}
A* createA()
{
   A a(2);
   return &a;
}
void createAonstack()
{
   A a(3);
}
int main(int argc, char const *argv[])
{
   A a;
   A * pa = &a;
   pa->display();
   createA(pa);
   pa->display();
   A * a2 = createA();
   cout << a2->m_i << endl;
   createAonstack();
   cout << a2->m_i << endl;
   return 0;
}
The results of the program above is
0
0
2
3
How to explain the result 2 and 3? From my understanding, the object created in function createA() should be deconstructed, and the pointer it returns should point to NULL, but why a2->m_i can be 2. And the 3 is even more confusing, as it seems that the function createAonstack() has nothing to do with a2.
 
     
     
    