请大神帮忙看下这个程序解决方案

请大神帮忙看下这个程序
#include <iostream>
#include <stack>
using namespace std;

class A
{
public:
A(int element)
{
a = element;
cout<<"construct"<<a<<endl;
}
~A()
{
cout<<"end"<<a<<endl;
}
void print()
{
cout<<a<<endl;
}
private:
int a;
};

int main()
{
A a1(1),a2(2);
stack<A> stack1;
stack1.push(a1);
stack1.push(a2);
A &data = stack1.top();
stack1.pop();
data.print();
return 0;
}

请大神帮忙看下这个程序解决方案
有两个问题:
1、构造两个对象,构造函数调用2次,析构函数为什么会调用4次呢?
2、stack1.pop()后,a2对象被析构了,那为什么它的引用data还能够调用成员函数print()呢?析构后对象内存不应该释放才对嘛?
本人c++新手,还请大神们帮忙解答一下,谢谢!

------解决方案--------------------
1. 有两个对象是通过调用A类的复制构造函数生成的。

2. 
a、 此时a2还没有析构,它的作用域是main函数,在main函数结束时才会调用它的析构函数
b、 push 操作会复制对象,引用的并不是a2,而是stack中复制生成的副本
------解决方案--------------------

#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace std;

class A
{
public:
A(const A& T)
{
this->a = T.a;
cout<<"调用拷贝构造"<<endl;

this->pbuff = new char[20];
memset(this->pbuff,0,20);
strcpy(this->pbuff,T.pbuff);
}
A(int element)
{
a = element;
pbuff = new char[20];
memset(pbuff,0,20);
sprintf(pbuff,"test %d",a);
cout<<"construct"<<a<<endl;
}
~A()
{
cout<<"end"<<a<<endl;
delete pbuff;
}
void print()
{
cout<<a<<","<<pbuff<<",0x"<<this<<endl;
}
private:
int a;
char* pbuff;
};

int main()
{
{
A a1(1),a2(2);
a1.print();
a2.print();
stack<A> stack1;
stack1.push(a1);
stack1.push(a2);
A &data = stack1.top();
//data.print();
stack1.pop();
data.print();
}
return 0;
}


加了点代码,加深理解,push这里是发生了拷贝构造,加了个buff,pop后访问就有问题了,内存释放了
你写的可以访问,是对象析构后,内存还没有马上被重新写入,你可以在pop加一句char*p = new char[10000] 然后print试试