# Include <qtcore/qcoreapplication> # include <iostream> # include <stdio. h> using namespace STD; Class A {public: A (); static void getstaticnum (); // static function void getnum (); // common function void usestatic (); // call the static function Int J for a common function; // The static int I for a common variable member; // The static member variable}; int A: I = 0; // static member variable initialization A: A () {J = 2;} void a: getstaticnum () {I ++; cout <"WYZ ======== getstaticnum () I =" <I <Endl;} void a: getnum () {J ++; cout <"WYZ ======= getnum () I =" <I <Endl; cout <"WYZ ======== getnum () J =" <j <Endl;} void a: usestatic () {static int K = 0; // static variable k ++; cout <"WYZ ========= usestatic () k = "<k <Endl; getstaticnum ();} int main (INT argc, char * argv []) {qcoreapplication A (argc, argv); A aa1; aa1.getnum (); A Aa2; aa2.getnum (); A aaa1; aaa1.getstaticnum (); // static function call method 1 A: getstaticnum (); // method 2 of static function call a * AA = new A (); AA-> getnum (); AA-> usestatic (); Delete AA; return 0; return a.exe C ();}
Result:
Starting /root/qtdir/StaticFunc-build-desktop/StaticFunc...wyz=========GetNum() i=0wyz=========GetNum() j=3wyz=========GetNum() i=0wyz=========GetNum() j=3wyz=========GetStaticNum() i=1wyz=========GetStaticNum() i=2wyz=========GetNum() i=2wyz=========GetNum() j=3wyz=========UseStatic() k=1wyz=========GetStaticNum() i=3wyz=========UseStatic() k=2wyz=========GetStaticNum() i=4/root/qtdir/StaticFunc-build-desktop/StaticFunc exited with code 0