我正在通过 C++ Primer 中的练习进行练习,并找到了练习 2.32 的在线解决方案。
我知道以下代码是非法的,因为初始化无法从 int 转换为 int*:
int null = 0, *p = null;
但是,提到的两个解决方案是我没有提出的:
const int null3 = 0, *p3 = null3;
constexpr int null4 = 0, *p4 = null4;
Run Code Online (Sandbox Code Playgroud)
为什么在编译期间允许这些没有错误?我仍然期待 p3 和 p4 的初始化需要 & 来表示地址 (&null3, &null4)。
这是我的记事本文件中的代码:
#include <iostream>
int main()
{
// int null = 0, *p = null; // it is not legal; depending on intent there are two ways to fix (that I can think off atm)
{
int null …Run Code Online (Sandbox Code Playgroud)