#include<iostream>
using namespace std;
class Point{
public:
Point():x(0),y(0){
cout<<"调用默认构造函数"<<endl;
}
Point(int x,int y):x(x),y(y){
cout<<"调用构造函数"<<endl;
}
~Point(){
cout<<"调用析构函数"<<endl;
}
int getx() const {return x;
}
int gety() const {return y;
}
void move(int newx, int newy){
x = newx;
y = newy;
}
private:
int x,y;
};
int main (){
Point *ptr1 = new Point;// 动态分配Point对象 调用默认构造函数
delete ptr1;//删除对象,释放空间,自动调用析构函数
ptr1 = new Point(1,2);
delete ptr1;
return 0;
}
c++ 简单的动态分配内存空间
最新推荐文章于 2023-06-06 22:04:38 发布