申明和定义 ?

liyuan02 2006-08-27 07:44:35
请问 申明和定义 有什么区别 ? 怎么是用他们?  如 extern int a

这样的申明有什么作用 ?  谢谢!
...全文
476 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jixingzhong 2006-08-27
  • 打赏
  • 举报
回复
声明和定义还是有区别的,
声明本身不分配空间,
定义要分配,
是否初始化看具体操作 ....

但是在第一次声明的时候,
将被处理为定义 ...
只有后面遇到的其他声明,
才被处理为声明 ....
y81348016 2006-08-27
  • 打赏
  • 举报
回复
恩 这个问题我也比较糊涂 最近在看primer 没太搞明白
Arthur_ 2006-08-27
  • 打赏
  • 举报
回复
這個問題還是自己做一下比較好

補充一下;一般一個文件的全局變量比如int a=3,實際上完整的寫應該是extern int a=3;

如果不想其他的文件應用這個變兩要加static int a=3;
lj860603 2006-08-27
  • 打赏
  • 举报
回复
严格来说,变量的声明有两种情况。

一是需要建立存储空间的。例如:int a 在声明的时候就同时分配了存储空间。
这种也叫“定义性声明”。
二是不需要建立存储空间的 ,例如:extern int a ;
这种也叫“引用性声明”。

但平时一般说的声明是指第二种情况,也就是不需要分配空间的声明。
chenhu_doc 2006-08-27
  • 打赏
  • 举报
回复
2.3.5. 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.

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.

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:

extern int i; // declares but does not define i
int i; // declares and defines i



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.

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:

extern double pi = 3.1416; // definition



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.

Because an extern that is initialized is treated as a definition, any subseqent definition of that variable is an error:

extern double pi = 3.1416; // definition
double pi; // error: redefinition of pi



Similarly, a subsequent extern declaration that has an initializer is also an error:

extern double pi = 3.1416; // definition
extern double pi; // ok: declaration not definition
extern double pi = 3.1416; // error: redefinition of pi



The distinction between a declaration and a definition may seem pedantic but in fact is quite important.

In C++ a variable must be defined exactly once and must be defined or declared before it is used.






Any variable that is used in more than one file requires declarations that are separate from the variable's definition. In such cases, one file will contain the definition for the variable. Other files that use that same variable will contain declarations forbut not a definition ofthat same variable.

chenhu_doc 2006-08-27
  • 打赏
  • 举报
回复
参考 c++ primer 第四版 52页
OOPhaisky 2006-08-27
  • 打赏
  • 举报
回复
只有带extern的才叫声明,而且如果既有extern,又给出初始值,则就是定义了,如下:
extern int a = 4;//虽然有extern,但是又给出了初始值4,所以此处是定义
believefym 2006-08-27
  • 打赏
  • 举报
回复
楼上的,举个例子,啥叫声明(除了extern),还真不明白啥叫声明了
int a;
这个肯定是分配空间的
lj860603 2006-08-27
  • 打赏
  • 举报
回复
extern int a //这样是把变量a声明为外部变量
lj860603 2006-08-27
  • 打赏
  • 举报
回复
楼上的说错啦。

定义:编译器会为变量或函数分配内存,象int a = 1;//定义并初始化为1

声明:只是表明其存在,但没有分配内存这个过程。
believefym 2006-08-27
  • 打赏
  • 举报
回复
声明:
int a;
只分配了空间,没有初始化

定义:
int a = 1;

extern表示外部已经定义过该变量,直接使用即可而不需要重新分配空间、定义

64,643

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧