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 + +