变量定义与声明的区别
变量定义与声明的区别
关于定义与声明
**************************BEGIN**************************
变量定义与声明的区别
变量的声明有两种情况:
(1) 一种是需要建立存储空间的(定义、声明)。例如:int a在声明的时候就已经建立了存储空间。
(2) 另一种是不需要建立存储空间的(声明)。例如:extern int a其中变量a是在别的文件中定义的。
前者是"定义性声明(defining declaration)"或者称为"定义(definition)",而后者是"引用性声明(referncing declaration)"。从广义的角度来讲声明中包含着定义,但是并非所有的声明都是定义,例如:int a它既是声明,同时又是定义。然而对于extern a来讲它只是声明不是定义。一般的情况下我们常常这样叙述,把建立空间的声明称之为"定义",而把不需要建立存储空间称之为"声明"。很明显我们在这里指的声明是范围比较窄的,也就是说非定义性质的声明。
例如:在主函数中
int main()
{
extern int A; //这是个声明而不是定义,声明A是一个已经定义了的外部变量 //注意:声明外部变量时可以把变量类型去掉如:extern A; dosth(); //执行函数
}
int A; //是定义,定义了A为整型的外部变量(全局变量)
外部变量(全局变量)的"定义"与外部变量的"声明"是不相同的,外部变量的定义只能有一次,它的位置是在所有函数之外,而同一个文件中的外部变量声明可以是多次的,它可以在函数之内(哪个函数要用就在那个函数中声明)也可以在函数之外(在外部变量的定义点之前)。系统会根据外部变量的定义(而不是根据外部变量的声明)分配存储空间的。对于外部变量来讲,初始化只能是在"定义"中进行,而不是在"声明"中。所谓的"声明",其作用,是声明该变量是一个已在后面定义过的外部变量,仅仅是在为了"提前"引用该变量而作的"声明"而已。extern只作声明,不作定义。
用static来声明一个变量的作用有二:
(1) 对于局部变量用static声明,则是为该变量分配的空间在整个程序的执行期内都始终存在
(2) 外部变量用static来声明,则该变量的作用只限于本文件模块 *************************THE END*************************
**************************BEGIN**************************
变量定义与声明的区别
Declarations and Definitions
As we'll see in Section 2.9 (p. 67), C++ programs typically are composed of many files. In order for multiple files to access the same variable, C++ distinguishes between declarations and definitions.
就像我们在2.9 (p. 67)节看到的一样,典型的C++程序通常会由好多文件组成。为了使不同的文件都可以访问同一个变量,C++会区分变量的声明
(declarations)和定义(definitions)。
A definition of a variable allocates storage for the variable and may also specify an initial value for the variable. There must be one and only one definition of a variable in a program.
变量的定义(definitions)会为这个变量分配存储空间,并且可能会为其指定一个初始化的值。在程序里,一个变量必须有一个,也只能有一处定义(definitions)。
A declaration makes known the type and name of the variable to the program.
A definition is also a declaration: When we define a variable, we declare its name and type. We can declare a name without defining it by using the extern keyword. A declaration that is not also a definition consists of the object's name and its type preceded by the keyword extern:
变量的声明(declarations)会将变量的类型和名称传达给程序。当然,定义(definitions)也是一种声明:当我们定义一个变量的时候,我们当然也声明了他的名称和类型。我们可以通过使用“extern”关键字来声明(declarations)一个变量,而不用定义(definitions)它。声明(declarations)的形式就是在对象(变量)的名字和类型前面,加上关键字“extern”:
An extern declaration is not a definition and does not allocate storage. In effect, it claims that a definition of the variable exists elsewhere in the program. A variable can be declared multiple times in a program, but it must be defined only once.
变量定义与声明的区别
带“extern”关键字的语句属于声明(declarations),不是定义(definitions),他不会给变量分配内存。实际上,它是说明程序中的某处包含这个变量的定义。一个变量可以在程序中被声明(declarations)多次,但是只能被定义(definitions)一次。
A declaration may have an initializer only if it is also a definition because only a definition allocates storage. The initializer must have storage to initialize. If an initializer is present, the declaration is treated as a definition even if the declaration is labeled extern: 声明(declarations)时你可以给变量初始化。但是一旦你这样做,那么这句话也就变成了定义(definitions),因为只有在定义(definitions)的时候才会为变量分配内存。初始化的时候必然要为初始值分配存储空间。如果你在声明(declarations)的时候同时初始化了变量,即便“extern”关键字存在,这个语句也会认为是定义(definitions)。
Despite the use of extern, this statement defines pi. Storage is allocated and initialized. An extern declaration may include an initializer only if it appears outside a function.
不管有没有“extern”关键字存在,这条语句的作用也是定义(definitions)“pi”。变量已经被分配了内存,并且赋予了初始值。声明(declarations)只有在一种情况下可以被初始化,那就是当他被放置在函数外部的时候。
Because an extern that is initialized is treated as a definition, any subseqent definition of that variable is an error:
由于包含初始化的声明(declarations)语句会被认为是定义(definitions),所以如下的用法会被认为是错误的:
Similarly, a subsequent extern declaration that has an initializer is also an error:
变量定义与声明的区别
同样的,定义(definitions)后再使用同样的声明(declarations)也是错误的:
The distinction between a declaration and a definition may seem pedantic but in fact is quite important.
声明(declarations)和定义(definitions)之间的区别看似有些卖弄学问的嫌疑,但是其实是非常重要的。
Note In C++ a variable must be defined exactly once and must be defined
or declared before it is used.
笔记 在C++里,变量必须被定义一次,最多一次,至少一次,而且必须在使用前
定义(definitions)或者声明(declarations)。
Any variable that …… 此处隐藏:4197字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [专业资料]《蜜蜂之家》教学反思
- [专业资料]过去分词作定语和表语1
- [专业资料]苏州工业园区住房公积金贷款申请表
- [专业资料]保安管理制度及处罚条例细则
- [专业资料]2018年中国工程咨询市场发展现状调研及
- [专业资料]2015年电大本科《学前教育科研方法》期
- [专业资料]数字信号处理实验 matlab版 离散傅里叶
- [专业资料]“十三五”重点项目-虎杖白藜芦醇及功
- [专业资料]2015-2020年中国竹木工艺市场需求及投
- [专业资料]国际贸易理论与实务作业五:理论案例分
- [专业资料]财政部修订发布事业单位会计制度
- [专业资料]BCA蛋白浓度测定试剂盒(增强型)
- [专业资料]工程进度总计划横道图模板(通用版)
- [专业资料]七年级地理同步练习(天气与气候)
- [专业资料]X光安检机介绍火灾自动报警系统的组成
- [专业资料]衢州市人民政府办公室关于印发衢州市区
- [专业资料]经济全球化及其影响[1]
- [专业资料]质粒DNA限制性酶切图谱分析
- [专业资料]国家安全人民防线工作“六项”制度
- [专业资料]劳动力投入计划及保证措施
- 电子账册联网监管培训手册
- 人教版语文七年级上第1课《在山的那边
- 对我区担保行业发展现状的思考与建议
- 平面四边形网格自动生成方法研究
- 2016年党课学习心得体会范文
- 如何设置电脑定时关机
- 全球最美人妖排行榜新鲜出炉
- 社会实践调查报告及问卷
- Visual Basic习题集
- 《鱼我所欲也》课件2
- 浙江省会计从业资格考试试卷
- 全遥控数字音量控制的D 类功率放大器资
- 鞍钢宪法与后福特主义
- 电表的改装与校准实验报告(1)
- 2014年高考理科数学真题解析分类汇编:
- Windows 7 AIK 的使用
- 风电场全场停电事故应急处置方案
- 化工原理选填题题库(下)
- 关于产学研合作教育模式的学习与思考
- 西安先锋公馆项目前期定位报告




