Class Members and class member function pointers for C + +

Source: Internet
Author: User

Class member function pointers:

Used to access class member functions, which differ from general function pointers.

A class member function handles a class data member, declaring a class member function pointer at the same time, and also pointing out which class's function pointer is available. Calls are also invoked through the object .

For a static member function of a class, it is shared by the class object and can only handle static data members, so its function pointers can be used like normal function pointers .

1 classTest2 {3  Public:4     voidFunint);5     voidFunint)Const;//Overload function, plus a const limit6     Static voidFun_static (int);7 8     intm_data;9 };Ten  One voidTest::fun (inti) A { -cout <<" Fun"<< I <<Endl; - } the  - voidTest::fun (intIConst - { -cout <<"Const Fun"<< I <<Endl; + } -  + voidTest::fun_static (inti) A { atcout <<"Static"<< I <<Endl; - } -  - intMain () - { -     void(Test::* pfun) (int) = &Test::fun;//member function pointer, to add the class domain action, and the right side of the equal sign to add & take the function address (understood to take the class member address)   in     void(Test::* pfunconst) (int)Const= &Test::fun;///IBID., pointer to const version member function -     void(*pfunstatic) (int) = &Test::fun_static;a pointer to a static member function, without the addition of a field, or to the right of the equal sign without the & symbol. same way as normal function pointers to     int(Test::* pi) = &Test::m_data;//class data member pointer +  - Test A; the     ConstTest b;//Note! GCC compile times error, hint definition const variable requires display definition constructor; vs compile-time warning, compiler-generated default constructor initializes "const" automatic data for unreliable results *  $A.*pi =3;Panax Notoginseng     //b.*pi = 4; //const Object cannot modify data member -  theA.fun (3); //Output Fun 3  +B.fun (3);//Output const Fun 3,const object can only call the const member function A  the(A.*pfun) (3); //Call member function pointer via object, output fun 3  +(A.*pfunconst) (3); //Call a const version of a function with a normal object, output a const fun 3  -      $     //(B.*pfun) (3); Const object cannot call normal function $(B.*pfunconst) (3); //Output Const FUN 3  -  -Pfunstatic (3);//Output static fun 3, static member function pointer, can be called directly theTest::fun_static (3); //Ibid  . - Wuyi     return 0; the}

Class Members and class member function pointers for C + +

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.