我不明白两者的意义。
当前回答
《K&R》(第二版)中有一些非常明确的定义;这有助于把它们放在一个地方,并作为一个整体来阅读:
"Definition" refers to the place where the variable is created or assigned storage; "declaration" refers to the places where the nature of the variable is stated but no storage is allocated. [p. 33] ... It is important to distinguish between the declaration of an external variable and its definition. A declaration announces the properties of a variable (primarily its type); a definition also causes storage to be set aside. If the lines int sp; double val[MAXVAL] appear outside of any function, they define the external variables sp and val, cause storage to be set aside, and also serve as the declaration for the rest of that source file. On the other hand, the lines extern int sp; extern double val[]; declare for the rest of the source file that sp is an int and that val is a double array (whose size is determined elsewhere), but they do not create the variables or reserve storage for them. There must be only one definition of an external variable among all the files that make up the source program. ... Array sizes must be specified with the definition, but are optional with an extern declaration. [pp. 80-81] ... Declarations specify the interpretation given to each identifier; they do not necessarily reserve storage associated with the identifier. Declarations that reserve storage are called definitions. [p. 210]
其他回答
c++ 11更新
由于我没有看到与c++ 11相关的答案,这里有一个。
声明是定义,除非声明了/n:
opaque enum - enum X: int; 模板参数-模板参数- MyArray 参数声明- x和y在int add(int x, int y); 别名声明-使用IntVector = std::vector<int>; - static_assert(sizeof(int) == 4, "Yikes!") 属性声明(实现定义的) 空声明;
以上列表从c++ 03继承的附加子句:
函数声明- add in int add(int x, int y); Extern说明符包含声明或链接说明符- Extern int a;或extern "C"{…}; 类中的静态数据成员-类C中的x{静态int x;}; 类/struct声明- struct Point; typedef int int; 使用声明-使用std::cout; 使用命名空间NS;
模板声明是一种声明。如果模板声明定义了函数、类或静态数据成员,那么模板声明也是定义。
以下例子来自于区分声明和定义的标准,我发现这些例子有助于理解它们之间的细微差别:
// except one all these are definitions
int a; // defines a
extern const int c = 1; // defines c
int f(int x) { return x + a; } // defines f and defines x
struct S { int a; int b; }; // defines S, S::a, and S::b
struct X { // defines X
int x; // defines non-static data member x
static int y; // DECLARES static data member y
X(): x(0) { } // defines a constructor of X
};
int X::y = 1; // defines X::y
enum { up , down }; // defines up and down
namespace N { int d; } // defines N and N::d
namespace N1 = N; // defines N1
X anX; // defines anX
// all these are declarations
extern int a; // declares a
extern const int c; // declares c
int f(int); // declares f
struct S; // declares S
typedef int Int; // declares Int
extern X anotherX; // declares anotherX
using N::d; // declares N::d
// specific to C++11 - these are not from the standard
enum X : int; // declares X with int as the underlying type
using IntVector = std::vector<int>; // declares IntVector as an alias to std::vector<int>
static_assert(X::y == 1, "Oops!"); // declares a static_assert which can render the program ill-formed or have no effect like an empty declaration, depending on the result of expr
template <class T> class C; // declares template class C
; // declares nothing
为了理解名词,我们先来看看动词。
声明- - - - - - 宣布:正式宣布;宣告
定义- - - - - - 清晰完整地显示或描述(某人或某事
所以,当你声明某物时,你只需告诉它是什么。
// declaration
int sum(int, int);
这一行声明了一个名为sum的C函数,它接受两个int类型的参数并返回一个int。但是,您还不能使用它。
当你提供它的实际工作方式时,这就是它的定义。
// definition
int sum(int x, int y)
{
return x + y;
}
声明意味着给变量命名和类型(在变量声明的情况下),例如:
int i;
或者将名称、返回类型和参数类型赋给一个没有函数体的函数(在函数声明的情况下),例如:
int max(int, int);
而定义意味着给变量赋值(在变量定义的情况下),例如:
i = 20;
或者为函数提供/添加函数体(功能)被称为函数定义,例如:
int max(int a, int b)
{
if(a>b) return a;
return b;
}
许多时间声明和定义可以一起完成:
int i=20;
and:
int max(int a, int b)
{
if(a>b) return a;
return b;
}
在上述情况下,我们定义并声明变量i和函数max()。
c++中有一些有趣的边缘情况(在C中也有一些)。考虑
T t;
它可以是一个定义,也可以是一个声明,取决于T是什么类型:
typedef void T();
T t; // declaration of function "t"
struct X {
T t; // declaration of function "t".
};
typedef int T;
T t; // definition of object "t".
在c++中,当使用模板时,还有另一种边缘情况。
template <typename T>
struct X {
static int member; // declaration
};
template<typename T>
int X<T>::member; // definition
template<>
int X<bool>::member; // declaration!
最后一个声明不是定义。它声明了X<bool>的静态成员的显式特化。它告诉编译器:“如果要实例化X<bool>::member,那么不要从主模板实例化成员的定义,而是使用在其他地方找到的定义”。要使其成为定义,必须提供初始化式
template<>
int X<bool>::member = 1; // definition, belongs into a .cpp file.
声明:“在某处,存在一个foo。”
定义:“…就是这儿!”
推荐文章
- C多行宏:do/while(0) vs作用域块
- time_t最终的类型定义是什么?
- 我需要显式处理负数或零时,总和平方数字?
- 函数名周围的括号是什么意思?
- 用C语言创建自己的头文件
- 格式化IO函数(*printf / *scanf)中的转换说明符%i和%d之间的区别是什么?
- main()中的Return语句vs exit()
- 如果不是内存地址,C指针到底是什么?
- 我如何在Visual Studio中预处理后看到C/ c++源文件?
- 为什么在标准容器中使用std::auto_ptr<>是错误的?
- 保护可执行文件不受逆向工程的影响?
- 空括号的默认构造函数
- 从C语言的函数返回一个struct
- C99 'restrict'关键字的实际用法?
- 缓冲区是什么意思?