#include<iostream>
using namespace std;
class A
{
public:
A(){cout<<"A construction
";}//构造函数
~A(){cout<<"A Destruction
";}//析构函数
};
int main() {
cout<<"构造a
";
A a;
cout<<"构造pb
";
A *pb=new A;
cout<<"--------------
";
{
cout<<"构造c
";
A c;//生命周期在大括号内
cout<<"析构c
";
}
cout<<"------------------
";
cout<<"析构pb
";
delete pb;
cout<<"析构a
";
return 0;
return 0;}
结果:
构造a
A construction
构造pb
A construction
--------------
构造c
A construction
析构c
A Destruction
------------------
析构pb
A Destruction
析构a
A Destruction
--------------------------------
Process exited after 0.1611 seconds
请按任意键继续. . .