⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 类的静态成员函数.txt

📁 里面的代码是自己写的,参考书是thingking in c++,代码有详细的说明,对学习c++语法非常有帮助!
💻 TXT
字号:
/*本程序选自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();
}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -