【发布时间】:2021-05-07 18:07:14
【问题描述】:
是否允许在删除后使用指针的名称?
例如,此代码不会编译。
int hundred = 100;
int * const finger = &hundred;
delete finger;
int * finger = new int; // error: conflicting declaration 'int* finger'
这也不会:
int hundred = 100;
int * const finger = &hundred;
delete finger;
int finger = 50; // error: conflicting declaration 'int finger'
【问题讨论】:
-
指针本身没有被删除,只有它所指向的东西。所以指针仍然存在,你不能在同一范围内使用该名称来做不同的事情。另外请不要删除不是您使用
new创建的内容,这是等待发生的崩溃。 -
谢谢。现在我明白了。