C++ 请问这里哪里错了

来源:百度知道 编辑:UC知道 时间:2024/05/29 06:06:27
#include<iostream>
#include<string>
using namespace std;
void main()
{
const int *p=new int(1024);
delete p;
}
vc++运行之后出错
'delete' : none of the 2 overloads can convert parameter 1 from type 'const int *'
这是vc的问题吗
C++ primer4的例子是这样的

把const去掉就可以了。const修饰的指针应该不能改动了

把const去掉
#include<iostream>
#include<string>
using namespace std;
void main()
{
int *p=new int[1024];
delete [] p;
}

p 的类型是const int* 称为整型常量指针,int*是整形指针,指向所分配空间的首地址,当加上const后,就变成了常量,不允许改变的,为常量分配空间,这样就会报错的

const是定义常量的,所以不能那样写。指向常量的指针是一个指向常量的指针变量。如下
const char *p=“abcd”
不能使用new生成了。