C++中definition與declaration的區(qū)別
來源:程序員人生 發(fā)布時間:2016-02-28 11:27:57 閱讀次數(shù):3775次
翻了好幾篇關(guān)于definition與declaration和博客,都寫的很坑人 ,看來我得寫1篇了,避免很多新手1入門就被坑了。
definition是通過declaration完全的定義了實體,每一個declaration都是definition,除下面幾種情況。
1,存儲類標(biāo)識符extern開頭的或語言連接標(biāo)識符開頭,但未初始化的。
extern const int a; // declares, but doesnt define a extern const int b = 1;// defines b
extern "C" void f1(void(*pf)()); // declares a function f1 with C linkage,
2.沒有函數(shù)體的函數(shù)
int f(int); // declares, but doesnt define f
3.函數(shù)中未定義的參數(shù)
int f(int x); // declares, but doesnt define f and x int f(int x) { // defines f and x return x+a; }
4.類中聲明的靜態(tài)變量
struct S { // defines S int n; // defines S::n static int i; // declares, but doesnt define S::i }; int S::i; // defines S::i
5.只申明了類的名字,主要用在forward declaration和elaborated type中
struct S; // declares, but doesnt define S class Y f(class T p); // declares, but doesnt define Y and T (and also f and p)
6.模板參數(shù)
template<typename T> // declares, but doesnt define T
7.預(yù)先申明的模板
template<> struct A<int>; // declares, but doesnt define A
8.typedef聲明
typedef S S2; // declares, but doesnt define S2 (S may be incomplete)
9.alias申明
using S2 = S; // declares, but doesnt define S2 (S may be incomplete)
10.模糊enum的申明
enum Color : int; // declares, but doesnt define Col
11.Using-declaration
using N::d; // declares, but doesnt define d
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈