我不明白两者的意义。
当前回答
这听起来很俗气,但这是我能把这些条款直接记在脑子里的最好方法:
宣言:想象托马斯·杰斐逊发表演讲……“我在此声明这个foo在这个源代码中存在!!”
定义:想象一本字典,你正在查找Foo和它的实际意思。
其他回答
从C99标准,6.7(5):
声明指定一组标识符的解释和属性。标识符的定义是该标识符的声明:
对于一个对象,使存储空间为该对象保留; 对于函数,包括函数体; 类的(唯一)声明 标识符。
来自c++标准3.1(2):
声明是定义,除非它声明了一个函数而没有指定函数体,它包含extern说明符或链接规范,但既没有初始化式也没有函数体,它在类声明中声明了静态数据成员,它是类名声明,或者它是typedef声明,using-declaration,或using-directive。
下面是一些例子。
有趣的是(也许不是,但我有点惊讶)typedef int myint;是C99中的定义,但在c++中只是声明。
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.
从c++标准文档中添加定义和声明示例(来自3.1节声明和定义)
定义:
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
声明:
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 d
为了理解声明和定义之间的区别,我们需要查看程序集代码:
uint8_t ui8 = 5; | movb $0x5,-0x45(%rbp)
int i = 5; | movl $0x5,-0x3c(%rbp)
uint32_t ui32 = 5; | movl $0x5,-0x38(%rbp)
uint64_t ui64 = 5; | movq $0x5,-0x10(%rbp)
double doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
这只是定义:
ui8 = 5; | movb $0x5,-0x45(%rbp)
i = 5; | movl $0x5,-0x3c(%rbp)
ui32 = 5; | movl $0x5,-0x38(%rbp)
ui64 = 5; | movq $0x5,-0x10(%rbp)
doub = 5; | movsd 0x328(%rip),%xmm0 # 0x400a20
movsd %xmm0,-0x8(%rbp)
正如你所看到的,没有任何变化。
声明与定义不同,因为声明提供的信息仅供编译器使用。例如uint8_t告诉编译器使用asm函数movb。
看到:
uint def; | no instructions
printf("some stuff..."); | [...] callq 0x400450 <printf@plt>
def=5; | movb $0x5,-0x45(%rbp)
声明没有等价的指令,因为它不是要执行的东西。
此外,声明告诉编译器变量的作用域。
我们可以说,声明是编译器用来确定正确使用变量的信息,以及某些内存属于某个变量多长时间。
声明是在未分配值或对象的情况下创建原语或对象引用变量或方法。 int; Final int a;
定义意味着分别为值或对象赋值 Int a =10;
初始化意味着为各个变量或对象分配内存。
推荐文章
- 编程中的术语“上下文”?
- 通过SSH使用Eclipse完成远程项目
- int的最大值
- 什么是“参数依赖查找”(又名ADL,或“Koenig查找”)?
- 如何将文件指针(file * fp)转换为文件描述符(int fd)?
- C“int”的大小是2字节还是4字节?
- 多维数组如何在内存中格式化?
- printf()和puts()在C语言中的区别是什么?
- 断言是邪恶的吗?
- 下面这些短语在c++中是什么意思:0 -,default-和value-initialization?
- 有效,但毫无价值的语法在开关情况下?
- 有一个好的Valgrind Windows的替代品吗?
- gcc在哪里查找C和c++头文件?
- c++中size_t和int的区别是什么?
- 在C和c++中静态变量存储在哪里?