深入探讨函数指针

2019-07-02  本文已影响0人  Luliang

最近复习了一下C++ 的函数指针部分,在此记录一下,例子来源与《C++ premier plus》,理解后还是不难的。

// arfupt.cpp -- an array of function pointers
#include <iostream>
// various notations, same signatures
const double * f1(const double ar[], int n);
const double * f2(const double[], int);
const double * f3(const double *, int);
int main()
{
    using namespace std;
    double av[3] = { 1112.3, 1542.6, 2227.9 };

    // 函数指针
    const double *(*p1)(const double *, int) = f1;
    auto p2 = f2;  // 自动类型推导
    // pre-C++0x can use the following code instead
    // const double *(*p2)(const double *, int) = f2;
    cout << "Using pointers to functions:\n";
    cout << " Address  Value\n";
    cout << (*p1)(av, 3) << ": " << *(*p1)(av, 3) << endl;
    cout << p2(av, 3) << ": " << *p2(av, 3) << endl;
    // pa是一个函数指针数组
    const double *(*pa[3])(const double *, int) = { f1,f2,f3 };
    // but it does work for initializing to a single value
    // pb 是一个指向pa 第一个元素的指针
    auto pb = pa;
    // const double *(**pb)(const double *, int) = pa;
    cout << "\nUsing an array of pointers to functions:\n";
    cout << " Address  Value\n";
    for (int i = 0; i < 3; i++) {
        cout << pa[i](av, 3) << ": " << *pa[i](av, 3) << endl;
    }

    cout << "\nUsing a pointer to a pointer to a function:\n";
    cout << " Address  Value\n";
    for (int i = 0; i < 3; i++) {
        cout << pb[i](av, 3) << ": " << *pb[i](av, 3) << endl;
    }
    // 指向函数数组指针的指针
    cout << "\nUsing pointers to an array of pointers:\n";
    cout << " Address  Value\n";
    // easy way to declare pc 
    auto pc = &pa;
    // pre-C++0x can use the following code instead
   // const double *(*(*pc)[3])(const double *, int) = &pa;
    cout << (*pc)[0](av, 3) << ": " << *(*pc)[0](av, 3) << endl;
    // hard way to declare pd
    const double *(*(*pd)[3])(const double *, int) = &pa;
    // store return value in pdb
    const double * pdb = (*pd)[1](av, 3);
    cout << pdb << ": " << *pdb << endl;
    // alternative notation
    cout << (*(*pd)[2])(av, 3) << ": " << *(*(*pd)[2])(av, 3) << endl;
    // cin.get();
    return 0;
}
// some rather dull functions
const double * f1(const double * ar, int n)
{
    return ar;
}
const double * f2(const double ar[], int n)
{
    return ar + 1;
}
const double * f3(const double ar[], int n)
{
    return ar + 2;
}


上一篇下一篇

猜你喜欢

热点阅读