#include <iostream>
using namespace std ;
typedef const double* (*FUN[3])(const double *, int) ;
const double* call_001(const double ar[], int n) ;
const double* call_002(const double [], int) ;
const double* call_003(const double *, int) ;
int main(int argc, char *argv[])
{
const double *rst = NULL ; // 作为返回值使用
double num[10] = { 0.00, 1.11, 2.22, 3.33, 4.44, 5.55, 6.66, 7.77, 8.88, 9.99 } ;
const double* (*fp[3])(const double *, int) = { call_001, call_002, call_003 } ; // 定义函数指针数组 fp 并初始化为
for ( int i = 0; i < 3; i++ )
{
rst = (*fp[i])(num, 3) ;
cout << *rst << endl ;
}
cout << "--------------- 分界线 ---------------" << endl ;
FUN *fp_t = &fp ; // 此处定义了一个函数指针数组的指针
for ( int i = 0; i < 3; i++ )
{
rst = (*(*fp_t)[i])(num, 3) ;
// rst = (*fp_t)[i](num, 3) ; // It's ok ..
cout << *rst << endl ;
}
return 0 ;
}
const double* call_001(const double ar[], int n)
{
return &ar[n] ;
}
const double* call_002(const double ar[], int n)
{
return ar+n ;
}
const double* call_003(const double *ar, int n)
{
return ar+n ;
<span style="font-family:SimHei;font-size:14px;">}
</span>
该实例代码中定义了一个指针函数指针数组的指针,也是借助typedef关键字实现的,直接进行定义目前还未研究出来。
C++函数指针实例详解(篇四)
最新推荐文章于 2023-08-30 18:47:28 发布