MyH.h
class StaticConst
{
public:
StaticConst()// :abc(0) -----(1)error C2438: 'abc' : cannot initialize static class data via constructor
{
//abc = 0; -------(2) error: expression must be a modifiable lvalue
}
void Tell();
private:
static int const abc ;//= 45; -------(3)
};
MyH.cpp
#include "MyH.h"
#include <iostream.h>
//const int StaticConst::abc ;//= 40;-------(4)
void StaticConst::Tell()
{
cout<<abc<<endl;
}
void main()
{
StaticConst sc;
sc.Tell();
}
1,2皆错误, 3,4方式选择一个即可,其中方式3在vc6下不行,使用intel编译器可以
--------------------------------------------------------------------------------------
class NoDefFunc
{
public:
static int GetInt();
enum { value = sizeof (GetInt()) };
};
void main()
{
cout<< NoDefFunc::value<<endl;
}
Output: 4
static成员函数甚至不需要有函数定义,声明即可