工大后院

 找回密码
 加入后院

扫一扫,访问微社区

QQ登录

只需一步,快速开始

搜索
查看: 4454|回复: 24

[讨论]callback 函数问题

[复制链接]
发表于 2008-4-6 23:44 | 显示全部楼层 |阅读模式
在C里, 我们可以定义一个函数指针实现callback函数.
如定义有
void func1();
void func2();
void func3();

Function(int x,int y,void (*func)()){......}

那么可以这样调用:
main(){
Function(x,y,func1);
Function(x,y,func2);
Function(x,y,func3);
}

通过回调函数,使得主调函数不需理会回调函数的实现细节,而灵活地实现各种功能.

在C++里, 是怎样实现callback函数的???[em021]
发表于 2008-4-7 00:01 | 显示全部楼层
巧了,我也只会用c的。等楼下,学习。
回复

使用道具 举报

发表于 2008-4-7 01:14 | 显示全部楼层
其实应该在上层来说,是比较少用到所谓的回调函数,回调函数只是一个函数指针,一般在平台或者驱动上面用得很多,因为他不知道会具体会是怎么样的操作,但是会要求这个操作在这层进行会比较好,所以就留个函数的接口给具体的应用层,这样,具体的应用就可以根据自己的需要来实现,然后把函数的指针给下层....


不懂C++的callback的说,LZ说一下钩子吧.和callback函数的不同?
回复

使用道具 举报

发表于 2008-4-7 11:43 | 显示全部楼层
C++在这部分有什么不同吗?

[ 本帖最后由 iptton 于 2008-4-8 00:51 编辑 ]
回复

使用道具 举报

 楼主| 发表于 2008-4-8 00:14 | 显示全部楼层
今晚有个人说是用函数对象来实现.
他给了一段这样的代码:
  1. #include "stdafx.h"
  2.   #include  
  3.   using namespace std;
  4.   class Negate
  5.   {
  6.   public:
  7.   template T operator()(T t) const
  8.   {
  9.   return -t;
  10.   }
  11.   };
  12.   void Callback(int n, const Negate& neg) // 传递一个函数对象
  13.   {
  14.   n = neg(n); // 调用重载的 () 操作 来对 n 进行 negate 操作
  15.   cout << n << endl;
  16.   }
  17.   int main(int argc, char* argv[])
  18.   {
  19.   // 调用方式一
  20.   Callback(5, Negate());
  21.   // 调用方式二
  22.   Negate neg;
  23.   cout << neg(9.99999) << endl;
  24.   cout << neg(__int32(39999999)) << endl;
  25.   return 0;
  26.   }
复制代码
但我没弄懂....
回复

使用道具 举报

发表于 2008-4-8 00:52 | 显示全部楼层
和C语言版同样的呀...
只是用了C++的运算符重载而已...
回复

使用道具 举报

 楼主| 发表于 2008-4-8 08:55 | 显示全部楼层
IP详解一下...[em021]
回复

使用道具 举报

发表于 2008-4-8 13:29 | 显示全部楼层
#include < iostream >
using std::cout;
using std::endl;
class FuncObj{
        public:
        FuncObj(){
                cout<<"FuncObj constructed.."<<endl;
        }
        void operator()(int sum) const{
                cout<<"sum="<<sum<<endl;
        }
};
void testCallBack(int x,int y,const FuncObj &fo){
        //调用函数对象中的()函数
        //作用与 fo.operator()(x+y)相同
        fo(x+y);
}
int main(){
        //传递一个匿名的 FuncObj 实例
        testCallBack(23,543,FuncObj());
        return 0;
}
回复

使用道具 举报

发表于 2008-4-8 13:33 | 显示全部楼层
与C语言的传函数指针同一原理,
只是这里改成了传一个“具有函数功能”的对象
(函数对象类就是重载了 operator() 函数的类,使这个类的实例看起来像函数)
回复

使用道具 举报

发表于 2008-4-8 13:41 | 显示全部楼层
标准库里有不少这样的应用可以看看 stl_function.h
// 20.3.2 arithmetic
/** @defgroup s20_3_2_arithmetic Arithmetic Classes
*  Because basic math often needs to be done during an algorithm, the library
*  provides functors for those operations.  See the documentation for
*  @link s20_3_1_base the base classes@endlink for examples of their use.
*
*  @{
*/
/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct plus : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x + __y; }
};

/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct minus : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x - __y; }
};

/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct multiplies : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x * __y; }
};

/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct divides : public binary_function<_Tp,_Tp,_Tp> {
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x / __y; }
};

/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct modulus : public binary_function<_Tp,_Tp,_Tp>
{
  _Tp operator()(const _Tp& __x, const _Tp& __y) const { return __x % __y; }
};

/// One of the @link s20_3_2_arithmetic math functors@endlink.
template <class _Tp>
struct negate : public unary_function<_Tp,_Tp>
{
  _Tp operator()(const _Tp& __x) const { return -__x; }
};
/** @}  */
回复

使用道具 举报

发表于 2008-4-8 13:48 | 显示全部楼层
此版不適宜我路過,大家早抖
回复

使用道具 举报

 楼主| 发表于 2008-4-10 22:59 | 显示全部楼层
现在需求是:
用户提供一个callback函数, 并通过register到framework里.存入一个表里.
当framework接收到某事件时,就从表里取出callback函数并执行.
回复

使用道具 举报

发表于 2008-4-11 00:35 | 显示全部楼层
C++ Framework?
只接触过QT...
LS项目用的是什么FrameWork?
最近学PHP的框架,突然对框架很感兴趣。。
回复

使用道具 举报

 楼主| 发表于 2008-4-11 00:55 | 显示全部楼层
自己实现的...
汗呀.得我一个人设计.....
回复

使用道具 举报

发表于 2008-4-11 00:58 | 显示全部楼层
[em021] [em021] [em021]
某师兄毕业设计就是PHP框架


又借了 large-scale C++ 希望这次能看一半以上...
回复

使用道具 举报

发表于 2008-4-11 01:03 | 显示全部楼层
书名全称 large-scale c++ software design..

是不是要实现 Observer模式?
类似于java的 addEvenetListener ?
回复

使用道具 举报

 楼主| 发表于 2008-4-11 01:05 | 显示全部楼层
以前这里有个会员叫sasadong的,他对php研究得比较多.
可惜这里留不住他老人家.
回复

使用道具 举报

发表于 2008-4-11 01:10 | 显示全部楼层
有空再翻下他那张帖看看,现在用的框架是超轻量级的MVC,不看文档单单看源代码就可以理清框架架构了
http://www.google.cn/search?q=c% ... al&client=firefox-a
回复

使用道具 举报

 楼主| 发表于 2008-4-11 01:20 | 显示全部楼层
原帖由 iptton 于 2008-4-11 01:03 发表
书名全称 large-scale c++ software design..

是不是要实现 Observer模式?
类似于java的 addEvenetListener ?


类似吧.
回复

使用道具 举报

 楼主| 发表于 2008-4-11 01:46 | 显示全部楼层
多个用户可以对同一event进行register,也就是一对多的依赖关体系.
当"一"发生变化, 多个对此event感兴趣的user都将要有所行动.
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 加入后院

本版积分规则

QQ|Archiver|手机版|小黑屋|广告业务Q|工大后院 ( 粤ICP备10013660号 )

GMT+8, 2025-5-14 11:26

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表