里面的代码是自己写的,参考书是thingking in c++,代码有详细的说明,对学习c++语法非常有帮助!

源代码在线查看: 类的静态成员函数.txt

软件大小: 13 K
上传用户: llll45356874
关键词: thingking 代码 in
下载地址: 免注册下载 普通下载 VIP

相关代码

				/*本程序选自thinking in c++ P430
				* 本程序的目的是为了说明类的静态成员函数的使用情况。
				*记住:Because the static member functions have no this pointer,so they
				*      cannot neither access non-static data members nor non-static member functions.
				*      they can access only static data members and call only other static member functions.
				*/
				class X {
					int m_i;
					static int j;
				public:
					X( int ii=0 ) : m_i(ii) {
						//Non-static member function can access static member function 
						//or data:
				        j = m_i;
					}
				
					int val() const { return m_i;}
					
					static int incr() {
						//! ++ m_i; Error : static member function cannot access non-static
						// member data;
				
						return ++j; //ok! access static  member data; 
					}
				
					static int f(){
						//! val(); //error :static member function cannot access non-static
						           //function
						return incr(); //OK! call static member function;
					}
				};
				
				int X::j = 0;
				
				int main()
				{
					X x;
					X *xp = &x;
				
					// 静态成员函数的调用方法
					x.f();
					xp->f();
					X::f();
				}
				
				
							

相关资源