C++ ➢ 函数指针

1.1 函数指针的基础知识

怎么使用函数指针?来看看下面这三步。

1.1.1获取函数的地址

获取函数的地址很简单,只要获取函数名就可以了。

    think(pass);
    thought(pass());

来看看上面两个函数的区别,pass() 为函数。
think() 调用使得 think( ) 能在其内部调用 pass() 函数,而 thought() 首先调用 pass() 函数,然后将 pass() 的返回值传递给 thought() 函数。

1.1.2 声明函数指针

声明指向某种数据类型的指针时,必须指定指针指向的类型。同样,声明指向函数的指针,也必须指定指针指向的函数类型
例如,有一个函数原型如下:

	double pam(int);   // prototype
	//正确的指针声明
	double (*pf)(int); // pf points to a function that takes
						// one int argument and that
						// returns type double

pam 是函数,(*pf) 也表示函数,pf 就是指针函数。

提示:通常要声明指向特定类型的函数的指针,可以首先编写这种函数的原型,然后用 (*pf) 替换函数名。
这样 pf 就是这类函数的指针。

再看以下两个对比:

double (*pf)(int); // pf points to a function that takes
                   // one int argument and that
                   // returns type double
double *pf(int); // pf() a function that returns a pointer-to-double 或许可以理解为double* pf

后者意味着 pf 是一个返回指针的函数
正确地声明 pf 后,便可以给它赋值。

double pam(int);   // prototype
//正确的指针声明
double (*pf)(int); // pf points to a function that takes
                   // one int argument and that
                   // returns type double
 pf = pam;

1.1.3 使用指针来调用函数

pf = pam;
double x = pam(4);     //call pam() using the function name;
double y = (*pf)(5);   //call pam() using the pointer pf

1.2 函数指针示例

#include<iostream>
using namespace std;
double besty(int);
double pam(int);

void estimate(int lines, double (*pf)(int));

int main()
{
    int code;
    cout << "How many lines of code do you need? ";
    cin >> code;
    cout << "Here's Betsy's estimate: " << endl;
    estimate(code, besty);
    cout << "Here's pam's estimate: " << endl;
    estimate(code, pam);
    return 0;
}

double besty(int lns)
{
    return 0.05 * lns;
}

double pam(int lns)
{
    return 0.03 * lns + 0.0004 * lns * lns;
}

void estimate(int lines, double (*pf)(int))
{
    cout << lines << " lines will take ";
    cout << (*pf)(lines) << " hour(s)." << endl;
}

1.3 深入探讨函数指针

#include<iostream>
using namespace std;
const double * f1(const double * ar, int n);
const double * f2(const double ar[], int n);
const double * f3(const double ar[], int n);

int main()
{
    double av[3] = {1112.3, 1542.6, 2227.9};

    const double *(*p1)(const double *, int) = f1;
    const double *(*p2)(const double *, int) = f2;
    cout << "Using pointers to functions: " << endl;
    cout << "Address Value" << endl;
    cout << (*p1)(av,3) << ": " << *(*p1)(av,3) << endl;
    cout << p2(av,3) << ": " << *p2(av,3) << endl;

    const double *(*pa[3])(const double *, int) = {f1,f2,f3};
    const double *(*pb[3])(const double *, int) = {f1,f2,f3};
    cout << "Using an array of pointers to functions: " << endl;
    cout << "Address Values" << endl;
    for(int i = 0; i < 3; i++)
        cout << pa[i](av,3) << ": " << *pa[i](av,3) << endl;
    cout << "Using a pointer to a pointer to a function: " << endl;
    cout << "Address Value" << endl;
    for(int i = 0; i < 3; i++)
        cout << pb[i](av,3) << ": " << *pb[i](av,3) << endl;

    cout << "Using pointers to an array of function pointers:" << endl;
    cout << "Address Value" << endl;
    const double *(*(*pc)[3])(const double *, int) = &pa;
    cout << (*pc)[0](av,3) << ": " << *(*pc)[0](av,3) << endl;//看图示
    const double *(*(*pd)[3])(const double*, int) = &pa;
    const double * pdb = (*pd)[1](av,3);
    cout << pdb << ": " << *pdb << endl;
    cout << (*(*pd)[2])(av,3) << ": " << *(*(*pd)[2])(av,3) << endl;

    return 0;
}

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;
}

结果如下:

Using pointers to functions:
Address Value
0x28fed8: 1112.3
0x28fee0: 1542.6
Using an array of pointers to functions:
Address Values
0x28fed8: 1112.3
0x28fee0: 1542.6
0x28fee8: 2227.9
Using a pointer to a pointer to a function:
Address Value
0x28fed8: 1112.3
0x28fee0: 1542.6
0x28fee8: 2227.9
Using pointers to an array of function pointers:
Address Value
0x28fed8: 1112.3
0x28fee0: 1542.6
0x28fee8: 2227.9

Process returned 0 (0x0)   execution time : 0.820 s
Press any key to continue.

代码32行处的解释如下:指针

资料参考 C++ Primer Plus (第六版)中文版,仅学习使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值