类的静态成员有两种:静态成员变量和静态成员函数,语法是在普通成员变量和成员函数前加static关键字。
静态成员变量在本质上是全局变量。一个类,哪怕一个对象都不存在,其静态成员变量也是存在的。静态成员函数并不需要作用在某个具体的对象上,因此本质上是全局函数。设置静态成员的目的,是为了将某些和类紧密相关的全局变量和全局函数写到类里面,形式上成为一个整体,达到封装的效果,但其效果与定义全局变量/函数相同。
例1 通过类名调用类的普通成员函数与静态成员函数
#include <iostream> using namespace std; class CRect{ public: void setParam(){} static void show(){} }; int main() { CRect::setParam(); CRect::show(); return 0; }编译错误:error C2352: ‘CRect::setParam’ : illegal call of non-static member function 结论:不能通过类名来调用类的非静态成员函数 例2 通过类的对象调用类的静态成员函数与非静态成员函数
#include <iostream> using namespace std; class CRect{ public: void setParam(){} static void show(){} }; int main() { CRect crt; crt.setParam(); crt.show(); return 0; }编译通过 结论:可以通过类的对象来调用类的静态/非静态成员函数 例3 在类的静态成员函数中使用类的非静态成员变量
#include <iostream> using namespace std; class CRect{ public: void setParam(){} static void show() { cout<<m_width<<endl; } private: int m_width; }; int main() { CRect crt; crt.show(); return 0; }编译错误:error C2597: illegal reference to data member ‘CRect::m_width’ in a static member function 原因:静态成员函数属于整个类,在类实例化对象之前就已经分配空间了,而类的非静态成员必须在类实例化对象后才有内存空间,所以这个调用就会出错,就好比没有声明一个变量却提前使用它一样。 结论:类的静态成员函数中不能使用类的非静态成员 例4 类的非静态成员函数中使用静态成员变量
#include <iostream> using namespace std; class CRect{ public: void setParam()//非静态成员函数中改变了静态成员变量的值 { m_height = 10; cout<<m_height<<endl; } static void show() { } private: int m_width; static int m_height;//非静态成员变量 }; int CRect::m_height = 0; int main() { CRect crt; crt.setParam(); return 0; }编译通过 结论:类的非静态成员函数中可以使用静态成员变量(显而易见) 例5 在类的非静态成员函数中调用静态成员函数
#include <iostream> using namespace std; class CRect{ public: void setParam() { show(); } static void show() { cout<<"static member function"<<endl; } private: int m_width; static int m_height; }; int main() { CRect crt; crt.setParam(); return 0; }编译通过 结论:综合例4、5,可以知道类的非静态成员函数中既可以使用静态成员变量,又可以调用静态成员函数,即类的非静态成员函数可以使用类的静态成员 例6 类的静态成员函数调用非静态成员函数
#include <iostream> using namespace std; class CRect{ public: void setParam() { cout<<"non-static member function"<<endl; } static void show() { setParam(); } private: int m_width; static int m_height; }; int main() { CRect crt; crt.show(); return 0; }编译出错:error C2352: ‘CRect::setParam’ : illegal call of non-static member function 原因:没有声明一个函数却提前调用了它 结论:综合例3、6,可以得出类的静态成员函数中既不可以使用非静态成员变量,又不可以调用非静态成员函数,即类的静态成员函数不可以使用非静态成员。 例7 类的静态成员变量的使用
#include <iostream> using namespace std; class CRect{ public: void setParam() { cout<<"non-static="<<m_height<<endl; } static void show() { cout<<"static="<<m_height<<endl; } private: int m_width; static int m_height; }; int main() { CRect crt; crt.setParam(); crt.show(); return 0; }编译错误:error LNK2001: unresolved external symbol “private: static int CRect::m_height” (?m_height@CRect@@0HA) 原因:类的静态成员变量在使用前没有进行初始化,在类外且在main函数外使用int CRect::m_height = 0;初始化静态成员变量即可 结论:类的静态成员变量必须先初始化再使用。 例8 类的静态成员变量的初始化位置
class CRect{ public: private: static m_height; }; int CRect::m_height = 0;//A int main() { int CRect::m_height = 0;//B CRect crt; int CRect::m_height = 0;//C crt.setParam(); crt.show(); return 0; }在A,B,C三处分别初始化类的静态成员变量时只有A出初始化正确,B和C处初始化编译出错:error C2655: ‘m_height’ : definition or redeclaration illegal in current scope 结论:类的静态成员变量初始化的位置为类外且在main函数前(是否理解有误??)
https://www.cnblogs.com/codingmengmeng/p/5906282.html http://c.biancheng.net/view/165.html