Turn: The magical function of an array of functional pointers (I)

Source: Internet
Author: User
Tags case statement

Transferred from: http://blog.sina.com.cn/s/blog_4c78b35f010008hi.html

I encountered such a problem in the development of a software process, the front-level module passed to my binary data, the input parameters of char* buffer and int length,buffer is the first address of the data, length represents the size of this batch of data. The data is characterized by a variable length, a variable type, and the type of the data identified by the first byte (Buffer[0]), which has a total of 256 (2 8-square) possibilities. My task is to have to deal with every possible data type, and my module contains several functions that are treated like this in each function. If you follow the usual practice, you will write the following code:

1 voidMyFunction (Char* Buffer,intlength)2{3__int8 Nstreamtype = buffer[0]4   Switch(Nstreamtype)5   {6      Case 0:7function1 ();8        Break;9      Case 1:Ten     ...... One     Case 255: Afunction255 (); -        Break; -    } the}

If this method is written down, then in each of my functions, must make so many judgments, write the code must be very long, and each processing, have to make multiple judgments to find the correct processing function, code execution efficiency is not high. In response to the above problem, I think of using the function pointer array method to solve this problem.

The concept of function pointers, mentioned in the classic tutorial of Mr. Tam Hao's C language programming, is that in most cases we don't use it, we ignore its existence, and the function name is actually a pointer to the entry address of the function, but it differs from normal such as int*, double* pointers, Take a look at the following example to understand the concept of a function pointer:

1  intFuntionintXinty)2  voidMainvoid)3  {4    int(*fun) (intXinty)5    intA =Ten, B = -;6function (A, b)7Fun =function;8(*Fun ) (A, b);9    .......Ten}


Statement 1 defines a functional function whose input is two integers and returns an integer (input parameter and return value can be any other data type);

Statement 3 defines a function pointer, unlike the int* or double* definition pointer, where the definition of a function pointer must indicate the input parameter, indicating that it is a function pointer, and that the *fun must be enclosed in parentheses;

Statement 6 assigns function pointers to functions, provided that the input parameters and return values of the *fun and function must be persisted.

Statement 5 calls the function functio () directly.

Statement 7 is called a function pointer, which is equivalent.

Of course, from the above example can not see the advantages of function pointers, the purpose is to elicit the concept of function pointer array. We can tell from the above example that since the function name can be saved by a function pointer, it is also possible to define an array to hold several function names, which is the array of function pointers. The prerequisite for proper use of the function pointer array is that these are some of the problems that I face in my work can be resolved as follows:

256 processing functions (and their implementations) are defined first.

void Function0 (void);

......

void funtion255 (void);

Next, define the array of function pointers and assign values to the array.

void (*fun[256]) (void);

Fun[0] = Function0;

.....

FUN[255] = function ();

The last MyFunction () function can be modified as follows:

1  void MyFunction (charint  length)2  {3    __int8 nsteamtype = buffer[0]; 4    (*Fun[nstreamtype]) (); 5  }

As long as 2 lines of code, the completion of 256 statements to do, reducing the amount of work to write code, the Nstreamtype as an array subscript, from the Code execution efficiency, also higher than the case statement. If you want to do this in more than one function, the function pointer array will show its advantages.

function pointers and typedef

About the use of function pointers in C + + (contains a discussion of typedef usage)

(a) The application of simple function pointers.
 1  //  2  char  (* Pfun) (int   3  int  a) {return  ;}  4   Main ()  5   { 6  pfun = Glfun;  7  (*pfun) (2   8 } 

the first line defines a pointer variable pfun. First of all, as we mentioned earlier, "Form 1" recognizes that it is a pointer to a function, which is an int and the return value is a char type. There is only one sentence we cannot use this pointer, because we have not yet been assigned a value for it.

The second line defines a function glfun (). The function is exactly a function that returns char as an int parameter. We want to understand the function from the level of the pointer-the function name of the function is actually a pointer, and the function name points to the first address of the function's code in memory.

then there is the lovely main () function, the first of which you should be able to read-it assigns the address of the function Glfun to the variable pfun. The "*pfun" in the second sentence of the main () function is clearly the content of the address pointed to by Pfun, and of course the contents of the function Glfun () are taken out, and the given parameter is 2.

(ii) More intuitive and convenient use of typedef
1  //form 2:typedef return type (* new type) (parameter)2typedefChar(*ptrfun) (int);3Ptrfun Pfun;4  CharGlfun (intA) {return;}5  voidMain ()6  {7Pfun =Glfun;8(*pfun) (2);9}

The function of a typedef is to define a new type.

The first line defines a type of ptrfun, and defines the type as a pointer to a function that takes an int as a parameter and returns a char type. You can use the Ptrfun in the same way as with Int,char.

The second line of code enables the use of this new type to define the variable pfun, which can be used as if it were in Form 1.

(c) Using a function pointer in a C + + class:
1   //form 3:typedef return type (class name::* new Type) (parameter table)2   classCA3   {4    Public:5     CharLcfun (intA) {return; }6   };7CA CA;8typedefChar(CA::* ptrfun) (int);9Ptrfun Pfun;Ten  voidMain () One  { OnePfun =Ca::lcfun; -ca. (*pfun) (2); -}

Here, the pointer is defined and used with a "class restriction" or "object", which is used to indicate which class the function pointer is pointing to, and the class object here can be obtained using new. Like what:

1  New CA; 2  Pca-> (*pfun) (2); 3  Delete pca;

And this class object pointer can be a member variable inside the class, and you can even use the this pointer. Like what:

1  void ca::lcfun2 () 2   {3    (this->*m_pfun) (2); 4  }

In a word, the use of a class member function pointer must have a call of "->*" or ". *".

Turn: The magical function of an array of functional pointers (I)

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.