什么是真正不同的"const"?带有"no-const"的变量定义? [英] what real different "const" variable definition with "no-const"?

查看:74
本文介绍了什么是真正不同的"const"?带有"no-const"的变量定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在2个cpp文件中,如果我分别添加一个具有相同名称的变量定义.

 //  1.cpp 
 const   int  nTest =  1 ;
...
//  2.cpp 
 const   int  nTest =  1 ;


...
编译器将成功构建.但是如果我删除"const",它将报告错误!

可能的原因是

  const   int  nTest =  1 ; 



实际上不是变量定义,换句话说,"const int nTest"不是具有有效内存地址的left-var,而只是一种符号.
但是,如果原因确实如此,原来对"const"关键字的理解是错误的,这确实使我感到困惑.
我对"const"的理解只是为了使变量不可更改或不可更改.

解决方案

Const变量可以由编译器优化,如果不使用它的地址,它甚至可能不会为其分配任何存储空间,只需将常量值直接内联到代码中即可.如果您不使用const,那么它将无法进行此优化,并且在您的程序中将有2个目标文件导出相同的符号,因此可能导致链接时间冲突. -您可以通过更改C ++默认公共存储说明符来避免这种冲突,方法是将静态"说明符添加到至少一个变量中,或者将至少一个变量放入与静态方法相同的匿名命名空间{}中关键字.
请注意,这可能是一段时间以来不可用的C ++功能的副作用,即您现在可以将const整数定义放入头文件中,而不使用#define.这仅适用于整数类型.在使用此功能之前,每个人都使用枚举将整数常量放入头文件和类定义中.

您可能想知道其他一些cpp文件可能要与extern一起使用此"const int X",如果根本不从其目标文件中导出该文件,他们将如何处理.您可以强制导出它,并使用"extern"分配存储空间:

定义:

 外部  const   int  XX =  0 ; 



声明:

 外部  const   int  XX; 



常量值 [

在C中,常量值默认为外部链接,因此它们只能出现在源文件中.在C ++中,常量值默认为内部链接,这使它们可以出现在头文件中.


const关键字并不意味着不可变,而是只读的.因此,编译器确实在内存中定义了一个变量,但是阻止您的代码对其进行修改.程序中指向相同内存位置的其他部分实际上可能会在您不查找时更改该值.

在不使用编译时在链接时出错的原因是因为您两次定义了全局变量.之所以如此,是因为编译器通过查看是否用值初始化变量来区分全局变量的定义与仅对全局变量的引用.通过在两个位置初始化nTest,您正在尝试使用该名称定义两个全局变量.

应用const关键字时,您不再需要定义全局变量. const关键字表示本地链接".从角度来看,您可能希望将该定义包含在头文件中,这是很有意义的,而头文件也包含在许多.cpp文件中.因此,在使用版本的const关键字中,您基本上是在定义文件本地常量,因此在多个文件中使用相同的名称进行操作时不会收到错误消息. /blockquote>

In 2 cpp files, If I add a variable definition with the same name respectively.

//1.cpp
const int nTest=1;
...
//2.cpp
const int nTest=1;


...
The complier will build successfully. but if I delete "const " it will report an error!

May the reason is that

const int nTest=1;



is not actually a variable definition ,in other words, The "const int nTest" is not a left-var that has valid memory address, but only a kind of symbol.
But If the reason is really so, the original understand about the "const" keyword is wrong and really confusing me.
My understand about "const" is just to make the variable un-mtuable or un-changeable.

Any Good explanation?

解决方案

Const variables can be optimized out by the compiler, if you don''t use its address it might not even allocate any storage for it, just inlines the constant value into the code directly. If you don''t use const then it can''t do this optimization and in your program there will be 2 object files that export the same symbol so it can cause a link time collision. - You can avoid this collision by changing the C++ default public storage specifier by either adding the "static" specifier to at least one of the variables or putting at least one of the variables to an anonymous namespace{} that does the same as the static keyword.
Note that this might be the side effect of a C++ feature that wasn''t available for some time, that is you can put a const integer definition into a header file now instead of using a #define. This is true only for integer types. Before this feature everyone was using enums to put integer constants to header files and into class definitions.

EDIT: You might wonder that some other cpp files might want to use this "const int X" with extern, and how would they do it if it isn''t exported from its object file at all. You can force exporting it and allocate storage with "extern":

DEFINITION:

extern const int XX = 0;



DECLARATION:

extern const int XX;



EDIT:
Constant Values[^]
Somewhere near the beginning of the article:

Quote:

In C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.


The const keyword does not mean immutable, but read-only. Thus, the compiler does define a variable in memory, but prevents your code from applying modifications to it. Other parts of your program that refer to the same memory location might actually change the value while you are not looking.

The reason you get an error at link time when you compile without the const keyword comes from the fact that you are defining the global variable twice. That is so, because the compiler distinguishes a definition of global variable from a mere reference to a global variable by looking if you are initializing the variable with a value or not. By initializing nTest at two places your are trying to define two global variables by that name.

When applying the const keyword you are no longer defining a global variable at all. The const keyword implies "local linkage". That makes sense from view point that you might want to include that definition in a header file, which is included from many .cpp files. So, in your version with the const keyword you are basically defining a file-local constant and hence you don''t get an error message when you do that in multiple files and using the same name.


这篇关于什么是真正不同的"const"?带有"no-const"的变量定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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