happmaoo 发表于 2013-2-5 02:12:41

深入理解C/C++函数指针

<iframe align="top" marginwidth="0" marginheight="0" src="http://www.zealware.com/csdnblog01.html" frameborder="0" width="728" scrolling="no" height="90"></iframe><div class="postText">原文地址:http://blog.sina.com.cn/u/1082089673

函数指针数组的妙用


笔者在开发某软件过程中遇到这样一个问题,前级模块传给我二进制数据,输入参数为 char* buffer和 int length,buffer是数据的首地址,length表示这批数据的长度。数据的特点是:长度不定,类型不定,由第一个字节(buffer)标识该数据的类型,共有256(28 )种可能性。我的任务是必须对每一种可能出现的数据类型都要作处理,并且我的模块包含若干个函数,在每个函数里面都要作类似的处理。若按通常做法,会写出如下代码:

void MyFuntion( char* buffer, int length )
{
    __int8 nStreamType = buffer;

    switch( nStreamType )
    {
       case 0:
           function1();
           break;
       case 1:
       ......
       case 255:
           function255();
           break;
     }
}
页: [1]
查看完整版本: 深入理解C/C++函数指针