A declaration is the all-encompassing term for anything that tells the compiler about an identifier.
A definition defines the storage, value, body, or contents of a declaration. The difference between a declaration and a definition is that a declaration tells you an entity's name and the external view of the entity, such as an object's type or a function's parameters, and a definition provides the internal workings of the entity: the storage and initial value of an object, a function body, and so on.
--------<<C++ in a Nutshell>>
这段话应该把定义和声明说的很清楚了。
看如下代码
int main()
{
int a; //声明了a;但没有定义;a=0;定义a;
return a;
}
C新手可能会认为上述代码一点问题都没有。可是却犯了只声明(declaration)没有定义(definition)的错误。用visual studio 2005 编译这段代码会有以下提示:
Run-Time Check Failure #3 - The variable 'a' is being used without being defined.
定义以后再使用变量在其他语言中更为重要: 例如 c#,java; 这两个语言以引用类型居多,并不会在申明类型的同时在堆栈中分配内存,而分配的只是一个4字节的引用。
A definition defines the storage, value, body, or contents of a declaration. The difference between a declaration and a definition is that a declaration tells you an entity's name and the external view of the entity, such as an object's type or a function's parameters, and a definition provides the internal workings of the entity: the storage and initial value of an object, a function body, and so on.
--------<<C++ in a Nutshell>>
这段话应该把定义和声明说的很清楚了。
看如下代码
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
C新手可能会认为上述代码一点问题都没有。可是却犯了只声明(declaration)没有定义(definition)的错误。用visual studio 2005 编译这段代码会有以下提示:
Run-Time Check Failure #3 - The variable 'a' is being used without being defined.
定义以后再使用变量在其他语言中更为重要: 例如 c#,java; 这两个语言以引用类型居多,并不会在申明类型的同时在堆栈中分配内存,而分配的只是一个4字节的引用。