在回答这个问题时,我有点困惑。我们都知道,由于 C++ 复制语义,这可以正常工作:
int *some_obj = new int(42);
int a_copy = *some_obj;
delete some_obj;
printf("The answer is %d\n", a_copy);
但是这个呢?
int *some_obj = new int(42);
int& a_ref = *some_obj;
delete some_obj;
printf("The answer is %d\n", a_ref);
这是访问已删除的内存吗?
可能以各种形式多次询问,但这对谷歌不太友好。哎呀,我不能写一个像样的标题。