c/指针函3

来源:互联网 发布:淘宝关键词怎么优化 编辑:程序博客网 时间:2024/06/05 20:41
// test_function_p2.cpp : Defines the entry point for the console application.
//函数指针同样可以作为参数传递给函数的,下面的列子,仔细阅读你将会它的用处,稍加推理就可以很方便我们进行一些复杂的变成工作


#include "stdafx.h"
#include <IOSTREAM>
#include <STRING>
using namespace std;
int test(int);
int test2(int(*ra)(int),int);
int main(int argc, char* argv[])
{
cout<<test<<endl;
typedef int(*fp)(int);
fp fpi;
fpi=test;//fpi赋予test函数的内存地址
cout<<test2(fpi,1)<<endl;//这里调用test2函数的时候,把fpi所存储的函数地址(test的函数地址)传递给了test 的第一个形参
cin.get();
    return 0;
}
int test(int a){
    return a-1;
}
    //这里定义了一个名字为ra的函数指针
int test2(int(*ra)(int),int b){
    int c=ra(10)+b;//在调用之后,已经指向fpi所指向的函数地址鸡test函数
    return c;
}




0 0