C++ 中定义和声明之间的区别是什么?


在 C++ 中,声明和定义经常被混淆。在 C 中,声明意味着您向编译器告知其类型、大小以及在函数声明中告知其类型和参数大小的任何变量或用户定义类型或函数。声明不会在内存中为任何变量保留空间。

另一方面,定义意味着除了声明所做的一切之外,还会在内存中保留空间。你可以说“定义 = 声明 + 空间保留”。

以下是声明示例:

extern int a;                           // Declaring a variable a without defining it
struct _tagExample { int a; int b; };   // Declaring a struct
int myFunc (int a, int b);              // Declaring a function

以下是定义示例:

int a;
int b = 0;
int myFunc (int a, int b) { return a + b; }
struct _tagExample example;

更新于: 2019 年 7 月 30 日

2K+ 的浏览量

开启你的 职业生涯

完成课程获得认证

开始
广告
© . All rights reserved.