博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Some Const Objects Are Defined in Headers
阅读量:5890 次
发布时间:2019-06-19

本文共 2855 字,大约阅读时间需要 9 分钟。

 

Some Const Objects Are Defined in Headers

首先明确一点:const型变量的作用域为其定义的文件,在其它文件中无法调用。

example:

// file1: const int var1 = 10; int var2 = 20; // file2: #include <stdio.h> extern int var1; extern int var2; int main() { printf("%d", var1); printf("%d", var2); getchar(); return 0; } //error LNK2001: unresolved external symbol "int var1" ( error LNK1120: 1 unresolved externals

 

通常来说,const型的表达式是指其值能够在编译是确定的表达式。为了使不同的文件使用相同的const值,就要使得const的定义在每一个文件中都可见,因此要将其放在头文件中。因为const的作用于为当前文件,因此将其放在头文件中不会导致重复定义的错误。(一般情况下头文件中只放声明部分而不放定义部分,因为头文件可能会被多个文件调用从而导致重复定义)。

非常重要的一点是,当我们将const型变量定义在头文件中时,每一个文件都将有一个自己的const变量,名称相同、值也相同。

 

< C++ Primer >:

Recall that by default a const variable is local to the file in which it is defined .As we shall now see, the reason for this default is to allow const variables to be defined in header files.

In C++ there are places where constant expression is required. For example, the initialize of an enumerator must be a constant expression. We ‘ll see other cases that require constant expressions in later chapters.

Generally speaking, a const expression is an expression that the compiler can evaluate at compile-time. A const variable of integral type may be a constant expression when it is itself initialized from a constant expression. However, for the const to be a constant expression, the initialize must be visible to the compiler. To allow multiple files to use the same constant value, the const and its initialize must be visible in each file. To make the initialize visible, we normally define such consts inside a header file. That way the compiler can see the initialize whenever the const is used.

However, there can be only one definition for any variable in a C++ program. A definition allocates storage; all uses of the variable must be refer to the same storage. Because, by default, const objects are local to the file in which they are defined, it is legal to put their definition in a header file.

There is one important implication of this behavior. When we define a const in a header file, every source file that includes that header has its own const variable with the same name and value.

When the const is initialized by a constant expression, then we are guaranteed that all the variables will have the same value. Moreover, in practice, most compilers will replace any use of such const variables by their corresponding constant expression at compile time. So, in practice, there won’t be any storage used to hold const variables that are initialized by constant expressions.

When a const is initialized by a value that is not a constant expression, then it should not be defined in header file. Instead, as with any other variable, the const should be defined and initialized in a source file. An extern declaration for that const should be made in the header, enabling multiple files to share that variable.

转载地址:http://mkfsx.baihongyu.com/

你可能感兴趣的文章
数组、字符串常用方法
查看>>
前端面试指南
查看>>
【整理】18种推荐的CSS命名和书写规范
查看>>
new命令的原理
查看>>
人工智能:是拿什么向奇点迫近的?
查看>>
南京a货翡翠,天津a货翡翠
查看>>
css3 渐变
查看>>
如何在 JavaScript 中更好地使用数组
查看>>
阿里开发者招聘节 | 面试题01:如何实现一个高效的单向链表逆序输出?
查看>>
只想着一直调用一直爽, 那API凭证泄漏风险如何破?
查看>>
IT软件开发常用英语词汇
查看>>
开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
查看>>
Dagger2 中的 Scope
查看>>
4K超清,2500万人在线,猫晚直播技术全解读
查看>>
比特币现金是世界上最好货币的十大理由
查看>>
redis
查看>>
批量删除实现
查看>>
thinkphp_邮箱找回密码_已迁移
查看>>
oracle 启动归档模式
查看>>
MySQL读写分离实验
查看>>