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.