static关键字
1.static成员是类的组成部分,但不是任何对象的组成部分,所以,类的static成员函数没有this指针
2.static成员不是任何对象的组成部分,所以,static成员函数不能声明为const
3.static成员函数不能为虚函数
4.static数据成员只能在类外定义,double Account::interestRate = initRate();例外是:只要初始化式是一个常量表达式,整型const static数据成员就可以在类中初始化。static const int period =30;但是该数据成员仍必须在类外进行定义,成员的定义不必再指定初始值,const int Accout::period;
5.static数据成员的类型可以是该类所属的类类型,非static成员被限定为其自身类对象的指针或者引用,eg:
class Bar{
static Bar mem1;
Bar *mem2;
Bar mem3; //error
}
static 数据成员可用做默认实参,非static数据成员不能用作默认实参,因为它的值不能独立于所属的对象而使用。
class Screen{
public:
Screen& clear(char = bkground);
prvate:
static const char bkground = ‘#’;
}