首页 > 编程语言 > 详细

C++ 函数指针的定义方法及使用

时间:2015-08-25 14:10:18      阅读:197      评论:0      收藏:0      [点我收藏+]

 

int add(int a,int b){
    return a+b;
}

 

第一种,c语言通用。定义一个process_job函数指针类型,返回值为 int ,函数参数为int a,int b。使用用两种方法。

    typedef int (*process_job)(int a,int b);
    process_job a;
    a = add;

   cout << a(10,12) << endl;
    cout << (*a)(10,12) << endl; //OK

第二种,C++。使用,只有一种方法。

#include <functional>
typedef function< int(int,int)> task;
        task t = add;
    cout << t(22,23) << endl;     
       // cout << (*t)(22,23) << endl; error

 

C++ 函数指针的定义方法及使用

原文:http://www.cnblogs.com/cycxtz/p/4757075.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!