#include <iostream>
using namespace std;
class Myclass{
private:
int* datas;
public:
Myclass(int size){
datas = new int[size];
}
~Myclass()
{
cout<<"析构函数被调用"<<endl;
delete [] datas;
}
};
int main()
{
cout << "Hello World!" << endl;
//第一次析构函数调用:局部对象作用域结束
Myclass m1(5);
Myclass* m2 = new Myclass(10);
return 0;
}
#include <iostream>
using namespace std;
class Myclass{
private:
int* datas;
public:
Myclass(int size){
datas = new int[size];
}
~Myclass()
{
cout<<"析构函数被调用"<<endl;
delete [] datas;
}
};
int main()
{
cout << "Hello World!" << endl;
//第一次析构函数调用:局部对象作用域结束
Myclass m1(5);
Myclass* m2 = new Myclass(10);
//第二次析构函数调用:delete删除内存
delete m2;
return 0;
}