The class member function pointer is similar to the class data member pointer. In development and Apache source code, there are exquisite applications of function pointer arrays.
This article mainly describes how to use function pointer arrays to reduce a large amount of repetitive work and improve code readability and efficiency.
// Declare the class containing the public member function
Class X
{
Public:
Int value;
Int total;
Int count;
Void setvalue (INT v) {value = V ;}
Void settotal (INT t) {Total = T ;}
Void setcount (INT c) {COUNT = C ;}
};
FP function [] =
{
& Amp; X: setvalue,
& Amp; X: settotal,
& Amp; X: setcount
};
// Define and initialize an array pointing to the class member function pointer
// Define a return value as void, with an int pointing to a function pointer
Typedef void (X: * FP) (INT );
// Create a Global Object
X;
// Use a pointer to a class member function
Void setmember (void (X: * Foo) (INT), int V)
{
// Pay attention to using the function pointer of the object. * You can-> *. For example: (A. * function [0]) (INT)
(A. * Foo) (v );
}
Void main ()
{
// Use the global object and pointer Array
Setmember (function [0], 5 );
Setmember (function [1],-13 );
Int start = 10;
Setmember (function [2], start );
}