c++ - 这是如何运作的?指向指针赋值的指针
问题描述
#include <iostream>
using namespace std;
int main() {
int *p1;
p1 = new int;
int *p2;
p2 = new int;
p2 = p1; // what happens here?
*p1=5;
cout << "pointer 2 is " << *p2 << endl << *p1 << endl; // both give out 5
delete p1; // what happens to p2 ?
cout << "pointer 2 is " << *p2 << endl;
delete p2;
return 0;
}
当我删除指针对象 p1 时会发生什么?现在指针 p2 引用的是什么?有人可以解释一下吗?谢谢您的帮助
解决方案
推荐阅读
- c# - C# 关系数据库
- android - 如何以编程方式触发低电量屏幕
- python - 将 DICOM 数据加载到 numpy 错误
- angular - Angular 7 和 Phantom JS:没有任何浏览器的 Jenkins 上的 0 个测试中的 0 个
- swift - 具有 IBInspectable 颜色属性的子类 - 无法覆盖
- visual-studio-2017 - 我无法将 Windows SDK 版本更改为 10.0.17763.0 ,当我单击应用时它只是重置回 8.1
- python - Spyder 是唯一具有适当变量资源管理器的 Python IDE 吗?
- ios - 单击以打开 url 时如何修复“结构”以包含 UIButton?
- php - 使用 PHPExcel 读取 Excel 并使用 MeekroDB 插入数组
- javascript - 使用单选按钮在 javascript 中编写代码并使用 if else 循环来计算简单和/或复利