当前位置导航:炫浪网>>网络学院>>编程开发>>C++教程>>C++进阶与实例

C++中回调(CallBack)的使用方法

回调函数是一个很有用,也很重要的概念。当发生某种事件时,系统或其他函数将会自动调用你定义的一段函数。回调函数在windows编程使用的场合很多,比如Hook回调函数:MouseProc,GetMsgProc以及EnumWindows,DrawState的回调函数等等,还有很多系统级的回调过程。 一般情况下, 我们使用的回调函数基本都是采用C语言风格。 这里介绍一种C++风格的回调对象方法。 采用template实现。


 view plaincopy to clipboardprint?
template < class Class, typename ReturnType, typename Parameter >
class SingularCallBack
{

public:

typedef ReturnType (Class::*Method)(Parameter);

SingularCallBack(Class* _class_instance, Method _method)
{
//取得对象实例地址,及调用方法地址
class_instance = _class_instance;
method = _method;
};

ReturnType operator()(Parameter parameter)
{
// 调用对象方法
return (class_instance->*method)(parameter);
};

ReturnType execute(Parameter parameter)
{
// 调用对象方法
return operator()(parameter);
};


private:

Class* class_instance;
Method method;

};
template < class Class, typename ReturnType, typename Parameter >
class SingularCallBack
{

public:

typedef ReturnType (Class::*Method)(Parameter);

SingularCallBack(Class* _class_instance, Method _method)
{
//取得对象实例地址,及调用方法地址
class_instance = _class_instance;
method = _method;
};

ReturnType operator()(Parameter parameter)
{
// 调用对象方法
return (class_instance->*method)(parameter);
};

ReturnType execute(Parameter parameter)
{
// 调用对象方法
return operator()(parameter);
};


private:

Class* class_instance;
Method method;

};

    示例:

    以下是两个类实现。

 view plaincopy to clipboardprint?
class A
{

public:

void output()
{
std::cout << "I am class A :D" << std::endl;
};

};

class B
{

public:

bool methodB(A a)
{
a.output();
return true;
}

};
class A
{

public:

void output()
{
std::cout << "I am class A :D" << std::endl;
};

};

class B
{

public:

bool methodB(A a)
{
a.output();
return true;
}

};

    SingularCallBack的各种调用示例:

 view plaincopy to clipboardprint?
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if((*cb)(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if((*cb)(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}


view plaincopy to clipboardprint?
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if(cb->execute(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;

SingularCallBack< B,bool,A >* cb;
cb = new SingularCallBack< B,bool,A >(&b,&B::methodB);

if(cb->execute(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}


view plaincopy to clipboardprint?
A a;
B b;
SingularCallBack< B,bool,A >cb(&b,&B::methodB);

if(cb(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}
A a;
B b;
SingularCallBack< B,bool,A >cb(&b,&B::methodB);

if(cb(a))
{
std::cout << "CallBack Fired Successfully!" << std::endl;
}
else
{
std::cout << "CallBack Fired Unsuccessfully!" << std::endl;
}

    view plaincopy to clipboardprint?

共2页 首页 上一页 1 2 下一页 尾页 跳转到
相关内容
赞助商链接