首页 > 编程语言 > 详细

C++中definition与declaration的区别

时间:2015-10-23 12:02:13      阅读:237      评论:0      收藏:0      [点我收藏+]

翻了好几篇关于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<typename T> // declares, but doesn‘t define T

7.预先申明的模板

template<> struct A<int>; // declares, but doesn‘t define A<int>

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



版权声明:本文为博主原创文章,未经博主允许不得转载。

C++中definition与declaration的区别

原文:http://blog.csdn.net/lastsweetop/article/details/49336221

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!