您的位置:首页 > 编程语言 > C语言/C++

C++ 声明和定义

2016-05-10 22:15 295 查看

定义和声明

定义,声明

定义:完全定义声明中的实体,往往需要分配内存空间

声明:简单声明实体。

大部分声明都是定义,除了下面几种情况。

extern修饰的,并且没有初始化的变量

extern const int a; // declares, but doesn't define a
extern const int b = 1; // defines b


没有函数体的函数声明

int f(int); // declares, but doesn't define f


在class定义中的static成员变量声明

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


class 声明(前向声明)

class 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)


模板参数声明

template<typename T> // declares, but doesn't define T


模板特化

template<> class A<int>; // declares, but doesn't define A<int>


typedef

typedef S S2; // declares, but doesn't define S2 (S may be incomplete)


定义,只能出现一次(One definition Rule)

任何变量,函数,类,模板,枚举类型在一个编译单元中,只能定义一次,但是,可以多次声明。

非内联函数和变量,在整个程序中,只能定义一次(不只是编译单元)(这里的整个程序,包括标准库和用户自定义库)。
内联函数,在一个编译单元里,只能定义一次。
class,在一个编译单元中,也只能定义一次。
class type, enumeration type, inline function with external linkage, class template, non-static function template, static data member of a class template, member function of a class template, partial template specialization ,满足一定条件,可以在一个编译单元中,定义一次。
对于,在整个程序中只能定义一次的东西,我们通常写到.cpp文件中;而,对于,在一个编译单元中只能定义一次的东西,我们往往写在.h文件中。

注:上面提到的,在一个编译单元中只能定义一次的,用了一个“满足一定条件”,这个条件比较复杂。但是,如果,我们将定义写在.h文件中,通常就满足了这个条件

什么情况下,需要有定义

简单地说,一个对象如果需要获取它的地址,或者有引用绑定它,则,必须要定义这个对象;一个函数,如果有代码调用它,就必须要定义这个函数。如果需要定义一个变量或者函数,然而,在整个程序中并没有定义它,则,程序会产生链接错误。

class A{
public:
static const int num=10;   //declare num
};
int main(){
int a = A::num;           //not need define A::num
const int& ra = A::num;   //need define A::num
return 0;
}

严格来说,

如何判断一个变量是否需要定义

变量会出现在很多表达式中,如果其中有一个表达式需要定义改变量,程序就需要定义这个变量;否则,不需要定义这个变量。

判断变量x在表达式ex出现,是否需要定义x?
如果同时满足下面两个条件,就不需要定义x,否则,定义x。

对x做左值转换为右值,不调用任何其他函数,并会产生一个常量表达式。

ex包含的潜在计算x的表达式,或者抛弃其计算结果,或者做左值到右值转换。

struct S { static const int x = 1; };
int f() { return S::x; } // does not need define S::x

or,

struct S { static const int x = 1; };
void f() { &S::x; } // discarded-value expression does not need define S::x


如何判断一个函数是否需要定义

以下几种情况需要定义函数:

如果一个函数被调用,显式或隐式。
虚函数,(非纯虚函数)(因为需要构建虚表)
如果new出现(显式或隐式),需要定义allocation 或 deallocation 函数
如果delete出现(显示或隐式),需要定义deallocation 函数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ 声明 定义