c++ - C 中的 const 与 C++ 中的 const

标签 c++ c constants

给定的代码在 C 中编译,但在 C++ 中失败。

int main()
{
   const int x; /* uninitialized const compiles in C but fails in C++*/
}

从 C 到 C++ 的转变背后的基本原理和原因是什么?

最佳答案

请参阅兼容性附录 C.1.6 中的规范:

7.1.6 [see also 3.5]

Change: const objects must be initialized in C++ but can be left uninitialized in C

Rationale: A const object cannot be assigned to so it must be initialized to hold a useful value.

Effect on original feature: Deletion of semantically well-defined feature.

Difficulty of converting: Semantic transformation.

How widely used: Seldom.

关于c++ - C 中的 const 与 C++ 中的 const,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6087729/

相关文章:

c++ - #define MY_INT 与 const int MY_INT

perl - 我可以对要迭代的常量进行分组吗?

c++ - 如何在构造函数中初始化类的成员数组?

c - 从结构队列中删除所有节点

c - 将指向 const 类型的指针初始化为非常量类型

c - C 中的全缓冲​​、行缓冲和无缓冲是什么意思?

c - 为什么 gcc(或 glibc)不实现 _s 函数?

c++ - 可用于存储和管理整数集合的最佳 C++ 数据结构是什么?

c++ - 如何在 CUDA 的核函数中将两个 openCV 矩阵相乘?

c++ - 列表初始化是隐式转换吗?