Function pointer instance

Source: Internet
Author: User

I am not an expert, but a common programmer. Through examples, we will discuss the usage of function pointers. because many of the projects involved use function pointers, it is necessary to write an example to familiarize yourself with the sum.


Generally, function pointers are used in projects to conveniently call functions, reduce Code complexity, and facilitate maintenance. Therefore, each function is stored first, for example, the hash data structure of a key-value pair is used to store the names and pointers of related functions. When used, the corresponding function pointer is retrieved Based on the function name, and then the corresponding function pointer can be called.


First, let me explain how to define a function pointer.

INT (* f) (int x);/* declare a function pointer */

Relatively, I wrote a macro definition: typedef void (* pfunc_example) (void * pobj, char * data, int length );

If this function is generally defined as void func_example (void
* Pobj, char * data, int length );

The differences between the two can be compared.


To store function pointers, we need to use the hash data structure. Here we use the STL Map

The header file is # include <map>

I wrote a macro definition: typedef Map <char *, pfunc_example> udt_map_char_pfunc_example;


Now I have written a function that we want to call. According to the actual situation, I can write many

Class cfuncpointex
{
Public:
Static void func_a (void * pobj, char * data, int length );
Static void func_ B (void * pobj, char * data, int length );
Static void func_c (void * pobj, char * data, int length );
};

Easy to implement

Void cfuncpointex: func_a (void * pobj, char * data, int length)
{
Printf ("func_a \ n ");
}

Void cfuncpointex: func_ B (void * pobj, char * data, int length)
{
Printf ("func_ B \ n ");
}

Void cfuncpointex: func_c (void * pobj, char * data, int length)
{
Printf ("func_c \ n ");
}


Then write a management class

Class cfuncmanager
{
Public:
Cfuncmanager (void );

Void registerfunc (char * func_name, pfunc_example pfunc_ex );
Pfunc_example getfunc (char * func_name );
PRIVATE:
Udt_map_char_pfunc_example pfunc_map;
};

Implementation

Void cfuncmanager: registerfunc (char * func_name, pfunc_example pfunc_ex)
{
Pfunc_map.insert (pair <char *, pfunc_example> (func_name, pfunc_ex ));
}

Pfunc_example cfuncmanager: getfunc (char * func_name)
{
Return pfunc_map.find (func_name)-> second;
}


Then write the test code in the main function.

Cfuncmanager cfunc_m;
Cfunc_m.registerfunc ("func_a", & cfuncpointex: func_a );
Cfunc_m.registerfunc ("func_ B", & cfuncpointex: func_ B );
Cfunc_m.registerfunc ("func_c", & cfuncpointex: func_c );

(Cfunc_m.getfunc ("func_a") (null, null, 0 );
(Cfunc_m.getfunc ("func_ B") (null, null, 0 );
(Cfunc_m.getfunc ("func_c") (null, null, 0 );


The result is not unexpected. It should be printed.

Func_a

Func_ B

Func_c


Through this example, we can understand the usage of function pointers. As for the purpose, I have seen many projects that use function pointers more or less.

This is the summary here.


Final source code

//FuncPointEx.h#ifndef __FUNCPOINTEX__H__#define __FUNCPOINTEX__H__#endif#include <map>using namespace std;typedef void (* PFUNC_EXAMPLE)(void *pobj, char *data, int length);typedef map<char *, PFUNC_EXAMPLE> UDT_MAP_CHAR_PFUNC_EXAMPLE;class CFuncPointEx{public:static void Func_A(void *pobj, char *data, int length);static void Func_B(void *pobj, char *data, int length);static void Func_C(void *pobj, char *data, int length);};class CFuncManager{public:CFuncManager(void);void RegisterFunc(char *func_name, PFUNC_EXAMPLE pfunc_ex);PFUNC_EXAMPLE GetFunc(char *func_name);private:UDT_MAP_CHAR_PFUNC_EXAMPLE pfunc_map;};

// Funcpointex. cpp: defines the entry point of the console application. // # Include "stdafx. H "# include" funcpointex. H "Void cfuncpointex: func_a (void * pobj, char * data, int length) {printf (" func_a \ n ");} void cfuncpointex: func_ B (void * pobj, char * data, int length) {printf ("func_ B \ n");} void cfuncpointex: func_c (void * pobj, char * data, int length) {printf ("func_c \ n");} cfuncmanager: cfuncmanager (void) {} void cfuncmanager: registerfunc (char * func_name, pfunc_example pfunc_ex) {pfunc_map.insert (pair <char *, pfunc_example> (func_name, pfunc_ex);} pfunc_example cfuncmanager: getfunc (char * func_name) {return comment (func_name)-> second ;} int _ tmain (INT argc, _ tchar * argv []) {cfuncmanager cfunc_m; functions ("func_a", & cfuncpointex: func_a); functions ("func_ B", & cfuncpointex:: func_ B); cfunc_m.registerfunc ("func_c", & cfuncpointex: func_c); (cfunc_m.getfunc ("func_a") (null, null, 0 ); (cfunc_m.getfunc ("func_ B") (null, null, 0); (cfunc_m.getfunc ("func_c") (null, null, 0); System ("pause "); return 0 ;}

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.