[讨论]callback 函数问题
在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函数的??? 巧了,我也只会用c的。等楼下,学习。 其实应该在上层来说,是比较少用到所谓的回调函数,回调函数只是一个函数指针,一般在平台或者驱动上面用得很多,因为他不知道会具体会是怎么样的操作,但是会要求这个操作在这层进行会比较好,所以就留个函数的接口给具体的应用层,这样,具体的应用就可以根据自己的需要来实现,然后把函数的指针给下层....
不懂C++的callback的说,LZ说一下钩子吧.和callback函数的不同? C++在这部分有什么不同吗?
[ 本帖最后由 iptton 于 2008-4-8 00:51 编辑 ] 今晚有个人说是用函数对象来实现.
他给了一段这样的代码:#include "stdafx.h"
#include
using namespace std;
class Negate
{
public:
template T operator()(T t) const
{
return -t;
}
};
void Callback(int n, const Negate& neg) // 传递一个函数对象
{
n = neg(n); // 调用重载的 () 操作 来对 n 进行 negate 操作
cout << n << endl;
}
int main(int argc, char* argv[])
{
// 调用方式一
Callback(5, Negate());
// 调用方式二
Negate neg;
cout << neg(9.99999) << endl;
cout << neg(__int32(39999999)) << endl;
return 0;
}但我没弄懂.... 和C语言版同样的呀...
只是用了C++的运算符重载而已... IP详解一下...
#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;
}
与C语言的传函数指针同一原理,
只是这里改成了传一个“具有函数功能”的对象
(函数对象类就是重载了 operator() 函数的类,使这个类的实例看起来像函数) 标准库里有不少这样的应用可以看看 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; }
};
/** @}*/
此版不適宜我路過,大家早抖 现在需求是:
用户提供一个callback函数, 并通过register到framework里.存入一个表里.
当framework接收到某事件时,就从表里取出callback函数并执行. C++ Framework?
只接触过QT...
LS项目用的是什么FrameWork?
最近学PHP的框架,突然对框架很感兴趣。。 自己实现的...
汗呀.得我一个人设计.....
某师兄毕业设计就是PHP框架
又借了 large-scale C++ 希望这次能看一半以上... 书名全称 large-scale c++ software design..
是不是要实现 Observer模式?
类似于java的 addEvenetListener ? 以前这里有个会员叫sasadong的,他对php研究得比较多.
可惜这里留不住他老人家. 有空再翻下他那张帖看看,现在用的框架是超轻量级的MVC,不看文档单单看源代码就可以理清框架架构了
http://www.google.cn/search?q=c% ... al&client=firefox-a 原帖由 iptton 于 2008-4-11 01:03 发表 https://www.gdutbbs.com/images/common/back.gif
书名全称 large-scale c++ software design..
是不是要实现 Observer模式?
类似于java的 addEvenetListener ?
类似吧. 多个用户可以对同一event进行register,也就是一对多的依赖关体系.
当"一"发生变化, 多个对此event感兴趣的user都将要有所行动.
页:
[1]
2