"Summary" C + + static member functions and test cases

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.