函数指针Function Pointer

来源:互联网 发布:python split函数用法 编辑:程序博客网 时间:2024/05/29 13:47

今天想系统的学习一下函数指针机器用法

  资料来源http://www.newty.de/fpt/index.html

《The Function Pointer Tutorials》 written by Lars Haendel2005

 

 

一、Introduce

You can use them to replace switch/case statement,to realize your own late-binding or to implement callbacks

 

二、The Syntax of C and C++ Function Pointer

There are two different types of fuction pointer:On the one hand there are pointer to ordinary C  functions or static C++ member fuctions ,On the other hand there are pointers to non-static C++ member functions.The basic difference is that the pointers to  non-static C++ memeber functions need a hidden argument: The this-pointer to an instance of  the class. Always keep in mind that:This two kinds of function pointer are incompatible  with each other. 

 

三、How to return a function pointer

 

a、Direct Solution

//“Plus ”and "Minit" are two defined Functions that return float and

//take  two float

 

float  (*GetPtr(const char optc))(float,float)

{

        if(optc=='+')

            return Plus;

         else

             return  Minit;

}

b、Solution using typedef

 

typedef  float  (*pt2Fuc)(float ,float);

pt2Fun Getprt(const char optc)

{

        if(optc=='+')

            return Plus;

         else

             return  Minit;

}

 

 

原创粉丝点击