如何声明?

在成员函数、成员变量前加关键字static

static void setRate();
static int nNumOfStrings;

声明与初始化:

在类声明文件(.h)中声明,在包含类方法的文件(.cpp)中初始化。

注意:

  • 静态成员函数不能使用this指针:无论是非静态成员函数调用静态成员函数,还是静态成员函数调用非静态成员函数,都不可以使用this指针,否则编译时出错

     vs2005下提示error C2671: 'Account::rate' : static member functions do not have 'this' pointers
    
  • 静态成员函数不能为const,否则编译时出错

     vs2005下提示error C2272: 'initRate' : modifiers not allowed on static member functions
    
  • 静态成员函数不能为virtual,否则编译时出错。

     vs2005下提示error C2216: 'virtual' cannot be used with 'static'
    
  • 静态成员变量的类型可以是该成员变量所在的类,但非静态成员变量不可以



blog comments powered by Disqus

Published

07 January 2013

Tags