static成员变量在类的定义体中初始化

来源:互联网 发布:淘宝购物怎样货到付款 编辑:程序博客网 时间:2024/05/01 21:22
一般来说,类的static成员跟普通数据成员一样,不能在类的定义体中初始化。(至于为什么还没想明白),比如下例

 

class A{public:static const double b=0;};


但是有之中情况是例外的,就是整形的const static成员,比如下例

#include "stdafx.h"#include "iostream"using namespace std;class A{public:static const int b=0;};const int A::b;int _tmain(int argc, _TCHAR* argv[]){A a;cout<<a.b;system("pause");return 0;}

但是仍然要在类的定义体外定义,不必也不能指定初始化值

 

原创粉丝点击