1. Static member functions can be stored with normal function pointers, and ordinary member functions must be stored with class function pointers
Class a{public:static void Fun () {cout << ' Hello World ' << Endl;} Public:void fun2 () {}};int main () {void (*p) () = &a::fun;//with a normal function pointer, correct void (*P1) () = &a::fun2;//with a normal function pointer, error void (A: :* p2) () = &a::fun2;//correct, function pointer with class void (A::* p3) () = &a::fun;//error, static member function with class function pointer}
2. Static member functions can not call non-static member functions for two reasons, one static member function is better than non-static member function generation, at compile time the static member function has been generated, and the second static member function does not contain the this pointer
Class a{public:static void Fun () {cout << ' Hello World ' << endl;fun2 ();//error, static member function cannot call ordinary member function;}public:void Fun2 () {fun ();//correct, normal member function can call static member function}};
3. Static member functions cannot be declared as virtual, const, or volatile functions at the same time.
Class a{public:static void Fun () const//error {cout << "Hello World" << Endl;} static virtual void fun2 ()//error {}volatile static void Fun3 ()//error {}public:void fun2 () {}};
4. Static member functions can still be called when no objects are created
Class a{public:static void Fun () {cout << ' Hello World ' << Endl;} Public:void fun2 () {}};int main () {a::fun ();//static member functions are stored in the data segment, and return 0 can still be called when the class is not instantiated yet;
If there is any shortage, I hope to correct it.
This article is from the "Pawnsir It Road" blog, so be sure to keep this source http://10743407.blog.51cto.com/10733407/1749774
"Summary" C + + static member functions and test cases