Tag Archives: Function pointer array

How to use C + + function pointer array

2015-11-24

1、 Origin

Under what circumstances did you come up with an array of function pointers?Because there are a series of sequential functions in a program recently written, and the forms of these functions are basically the same, it is necessary to judge the execution result of each step, whether the execution is successful or not. If the execution is successful, the next function will continue to be executed. If the execution fails, the terminal will execute and exit.

Because there are many functions to be executed in turn, there are five. To write code according to the conventional way of thinking is to judge the result of each step by if-else, and decide the content of the next step according to the result. At present, there are only five functions to be executed. If there are more similar functions to be executed in sequence, this function will be very long, not easy to read, and the code will not be beautiful.

Considering that the parameter list and return value of the current function are basically the same, we consider whether we can traverse the function pointer array, store the function pointer in the array and traverse the array, so as to execute the function pointed to by the corresponding pointer in turn, judge the results one by one, and do the corresponding processing. In this way, the result processing part can adopt a unified way, which is very convenient to judge, the code is also very simple and clear, and it is easy to read.

So online query related information, to understand the use of function pointer array. Through data query, the use of C + + function pointer array can be divided into two categories, one is the use of global function pointer array (the same as C language), the other is the use of C + + member function pointer array. Now the two cases are sorted out respectively.

2、 The use of global function pointer array in C

(1) Requirements

1. The parameter list and return value type of a series of functions to be called should be identical;

2. It is required to put the function pointer to be called into the function pointer array (if the function must have sequential execution, then the storage order of the function pointer should be consistent with the calling order);

3. When calling, take out the function pointer to be executed from the array and execute the corresponding function.

(2) Code case

Reference from: http://blog.csdn.net/feitianxuxue/article/details/7300291

 

#include <iostream> 

using namespace std; 

 

void function0(int); 

void function1(int); 

void function2(int); 

 

int _ tmain(int argc, _ TCHAR* argv[]) 

Void (* f [3]) (int) = {function0, function1, function2};// save the three function pointers in the array F

      intchoice;

      cout<< “Enter a number between 0 and 2,3 to end: “;

      cin>> choice;

//handle user selection

      while((choice >= 0) && (choice <3))

      {

//call a function in the array f

(* f [choice]) (choice);// F [choice] select the pointer whose position is choice in the array.

//the pointer is dereferenced to call the function, and choice is passed as an argument to the function.

             cout<< “Enter a number between 0 and 2,3 to end: “;

             cin>> choice;

      }

      cout<< “Program execution completed.” << endl;

      system(“pause”);

      return0;

}

 

void function0(int a)

{

      cout<< “You entered” << a << ” so function0 wascalled\n\n”; 

}

 

void function1(int b)

{

      cout<< “You entered” << b << ” so function1 wascalled\n\n”;

}

 

void function2(int c)

{

      cout<< “You entered” << c << ” so function2 wascalled\n\n”;

}

3、 The use of pointer array of member function in C + + class

(1) Requirements

When using the pointer array of member function in C + + class, it is different from that of C. The requirements are as follows:

1. It is required that the parameter list and return value type of the member function put into the function pointer array are completely consistent;

2. Define a function pointer array, and specify the length of the array (equal to the number of member functions to be stored), and assign the member function pointer to the corresponding array variable during initialization (if the function must have sequential execution, the storage order of the function pointer should be consistent with the calling order);

3. When calling, take out the function pointer to be executed from the array and execute the corresponding function. When calling, use “& gt; *” or “. *” to point to array elements, otherwise an error will be reported during compilation.

(2) Code case

Reference from: http://www.cppblog.com/dragon/archive/2010/12/02/135250.html

 

/*

* small program for testing pointer array of member function

* key points of the code have been marked in red

 */

#include <iostream>

using namespace std;

 

class Test

{

public:

   Test();

   ~Test();

 

private:

   void add5(){ res+=5;}

   void add6(){ res+=6;}

//this 2 is very important. If it is not written in VC, an error will be reported, but it is not reported in QT, but an error will be reported during the deconstruction!

    void (Test::*add[2])();

public:

   void DoAddAction();

   void Display();

private:

   int res;

};

 

Test::Test()

{

//pay attention to the writing here

    add[0]=&Test::add5;

    add[1]=&Test::add6;

   res=0;

}

 

Test::~Test()

{

 

}

 

void Test::DoAddAction()

{

    for (int i=0;i<2;i++)

    {

//using class member function pointer must have a call of “& gt; *” or “. *”

        (this->*add[i])();

    }

}

 

void Test::Display()

{

   cout<<“The res is:”<<res<<endl;

}

 

int main()

{

   Test * test=new Test();

   test->DoAddAction();

   test->Display();

   delete test;

   return 0;

}

4、 Summary

The above methods illustrate the method of using function pointer array in C and C + + classes. It is obvious that using function pointer array can call functions more conveniently, make the code concise and beautiful, and provide a level of code quality.

In the future program development process, we encounter similar problems. When we can use the function pointer array, we can try to only use the function pointer array to make the development easier and more fun.