C + +: static member functions

Source: Internet
Author: User

1. The address of a static member function can be stored with a normal function pointer, whereas an ordinary member function address needs to be stored with a class member function pointer.

 1 class Base{ 2      Public: 3         Static intfunc1 () {};4         intFunc2 () {};5 }; 6  7  8 intMain ()9 {Ten     int(*PF1) () =&Base:: func1;//normal function pointers One     int(Base::* PF2) () =&Base:: FUNC2;//member function pointer A     return 0; -}

2. Static member functions can not invoke non-static members of a class. Because the static member function does not contain the this pointer.

3. Static member functions cannot be declared at the same time as virtual, const, and volatile functions.

4. Static member functions can be accessed without creating any instance objects.

 1#include <iostream>2 using namespacestd;3  4 classM5 { 6      Public: 7Minta) { 8A=A;9b+=A;}Ten         Static voidF1 (m); One     Private: A         intA; -         Static intB; - }; the  - voidm::f1 (m) - { -cout <<"a="<< M.A <<Endl; +cout <<"b="<< B <<Endl; - } +  A intm::b=0; at  - intMain () - { -M P (5), Q (Ten); -m::f1 (P); -m::f1 (Q); in     return 0; -}
[[email protected] test]$./a. out a=5b=a=tenb =

C + +: static member functions

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.