|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
我们知道new分配动态数组在释放的时候需要用delete运算符;
同时如果分配的是动态数组则应该写成:delete []指针名,但是为什么下边使用double类型的动态数组在释放的时候可以delete p或者delete []p都可以,但是Point类型动态数组只能写成delete []ptr ,
我试了一下delete ptr会报错,请各位大神普及一下知识
#include<iostream>
#include<string>
using namespace std;
class Point {
public:
Point(int = 0, int = 0);
~Point();
};
Point::Point(int a,int b)
{
cout << "gouzao" << endl;
}
Point::~Point()
{
cout << "xigou" << endl;
}
int main()
{
double *p;
p = new double[3];
for (int i = 0;i < 3;i++)
cout << *(p + 1) << " ";
delete p; //这里不会报错!!!!!
Point *ptr = new Point[2];
delete ptr; //这里报错,只能用delete[]ptr!!!!!!!
return 0;
}
|
|