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;
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP