常量用C VS用C ++常量 [英] const in C vs const in C++

查看:149
本文介绍了常量用C VS用C ++常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定的code编译在C,但在C未能++。

The given code compiles in C but fails in C++.

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

什么是理由,并从C到C ++的变化背后的原因是什么?

What is the rationale and the reason behind the change from C to C++?

推荐答案

查看规格,在兼容性附录C.1.6:

See the spec, in the compatibility appendix C.1.6:

7.1.6 [参见3.5]

7.1.6 [see also 3.5]

更改 const对象必须在C ++初始化,但可以留未初始化用C

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

理由: const对象不能被赋予所以它必须被初始化为举办一个有用的值

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.

转换的难度:语义转换

如何广泛用于:很少

这篇关于常量用C VS用C ++常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆