The pointer to the function is to use the pointer to save the address of the function as follows:
Void sub (int A, int B); int _ tmain (INT argc, _ tchar * argv []) {void (* t_vp) (int A, int B ); // define the function pointer t_vp = sub; // assign a value to the function pointer (* t_vp) (5, 6); // call the function pointer system ("pause"); Return 0 ;} void sub (int A, int B) {cout <(a + B );}
To assign a value to a function pointer, you need to explain the following step: In t_vp = sub, sub is the function name, which is a function type, not a function pointer. Here is the default conversion. T_vp = & sub, which indicates display conversion. However, the pointer to a class member function cannot be converted by default. The details are as follows:
# Include "stdafx. H "# include <iostream> using namespace STD; Class C {public: int addt (INT, INT) ;}; int _ tmain (INT argc, _ tchar * argv []) {c temp; int (C: * p) (int A, int B); P = & C: addt; // The converted int t_ia = (temp. * p) (5, 6); cout <t_ia <Endl; System ("pause"); Return 0;} int C: addt (int A, int B) {return (a + B );}
In C ++, the class name is added with two colons, for example, C: addt. Only when addt is a static member of class C, C :: addt can represent a left value.
The default conversion from the function type to the function pointer type is only available when the function type is left. All non-static member functions do not have this default conversion from the function type to the function pointer type, so display conversion is required.