翻了好幾篇關於definition與declaration和博客,都寫的很坑人 ,看來我得寫一篇了,避免很多新手一入門就被坑了。
definition是通過declaration完整的定義了實體,每個declaration都是definition,除了下面幾種情況。
1,存儲類標識符extern開頭的或者語言連接標識符開頭,但未初始化的。
extern const int a; // declares, but doesn't define a extern const int b = 1;// defines b
extern C void f1(void(*pf)()); // declares a function f1 with C linkage,
2.沒有函數體的函數
int f(int); // declares, but doesn't define f
3.函數中未定義的參數
int f(int x); // declares, but doesn't define f and x int f(int x) { // defines f and x return x+a; }
4.類中聲明的靜態變量
struct S { // defines S int n; // defines S::n static int i; // declares, but doesn't define S::i }; int S::i; // defines S::i
5.只申明了類的名字,主要用在forward declaration和elaborated type中
struct S; // declares, but doesn't define S class Y f(class T p); // declares, but doesn't define Y and T (and also f and p)
6.模板參數
template// declares, but doesn't define T
7.預先申明的模板
template<> struct A; // declares, but doesn't define A
8.typedef聲明
typedef S S2; // declares, but doesn't define S2 (S may be incomplete)
9.alias申明
using S2 = S; // declares, but doesn't define S2 (S may be incomplete)
10.模糊enum的申明
enum Color : int; // declares, but doesn't define Col
11.Using-declaration
using N::d; // declares, but doesn't define d