我不明白两者的意义。
当前回答
声明意味着给变量命名和类型(在变量声明的情况下),例如:
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()。
其他回答
声明意味着给变量命名和类型(在变量声明的情况下),例如:
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++标准3.1节:
声明将名称引入到翻译单元中,或者重新声明前一个翻译单元中引入的名称 声明。声明指定这些名称的解释和属性。
下一段声明(强调是我的)声明是一种定义,除非……
... 它声明一个函数而不指定函数体:
void sqrt(double); // declares sqrt
... 它在类定义中声明一个静态成员:
struct X
{
int a; // defines a
static int b; // declares b
};
... 它声明了一个类名:
class Y;
... 它包含没有初始化式或函数体的extern关键字:
extern const int i = 0; // defines i
extern int j; // declares j
extern "C"
{
void foo(); // declares foo
}
... Or是类型定义或using语句。
typedef long LONG_32; // declares LONG_32
using namespace std; // declares std
现在,理解声明和定义之间的区别很重要的一个重要原因是:一个定义规则。c++标准第3.2.1节:
任何翻译单元都不能包含任何变量、函数、类类型、枚举类型或模板的多个定义。
为了理解声明和定义之间的区别,我们需要查看程序集代码:
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)
声明没有等价的指令,因为它不是要执行的东西。
此外,声明告诉编译器变量的作用域。
我们可以说,声明是编译器用来确定正确使用变量的信息,以及某些内存属于某个变量多长时间。
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
声明说"这个东西存在于某个地方"
int sampleFunc(); // function
extern int car; // variable
定义说,这个东西存在于这里;为它留下记忆"
int sampleFunc() {} // function
int car; // variable
在定义对象时,初始化是可选的,并表示“这是这个东西的初始值”:
int car = 0; // variable
推荐文章
- 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'关键字的实际用法?
- 缓冲区是什么意思?