C++中回调函数使用详解(普通函数作为回调函数以及类的成员函数作为回调函数)

引言:

在C++编程,尤其时写Qt程序时,需要大量使用回调函数,在网上也有很多大牛对回调函数的使用进行了讲解,但是很多都是针对某一个或者两个特定的类型的。我在这篇文章中对网上关于回调函数的使用进行了归纳总结,并且对每一种类型的回调函数都写了一个简单的例子来实现,每个例子都可以在vs2015中编译通过。其中需要用到 bind和function的代码,由于使用了C++11的新标准,所以在vc6.0中可能没法直接编译通过。

一、普通函数作为回调函数

普通函数作为回调函数时,使用普通的函数指针进行传入。

例:

#include 
using namespace std;
typedef int (*pGeneralFun)(int, int);                // 定义函数指针
int sum(int a, int b){
    return a+b;
} 
void result(pGeneralFun fun, int a, int b){      // 定义接口韩式,在接口函数内实现回调函数的调用
    cout<<(*fun)(a, b)<

 

二、类的成员函数作为回调函数

1、类的静态成员函数作为回调函数

a、在类外定义接口函数

#include 
using namespace std;
typedef int(*pGeneralFun)(int, int);              // 类的静态成员函数的函数指针与普通函数一致
class Test{
public:
    static int Max(int a, int b){
        return a>=b? a:b;
    }
};
void result(pGeneralFun fun, int a, int b){    // 类的静态成员函数的接口函数定义与普通函数一样
    cout<<(*fun)(a, b)<

 

b、在类内定义接口函数

#include 
using namespace std;
typedef int(*pGeneralFun)(int, int);              // 类的静态成员函数的函数指针与普通函数一致
class Test{
public:
    static int Max(int a, int b){
        return a>=b? a:b;
    }
    void result(pGeneralFun fun, int a, int b){    // 在类内部定义接口函数,定义形式与类外一样
        cout<<(*fun)(a, b)<

 

c、在类A中定义回调函数,在类B中定义接口函数

#include 
using namespace std;
typedef int (*pGeneralFun)(int, int);
class A{
public:
    static int Max(int a, int b){             // 在类 A 中定义回调函数
        return a>=b?a:b;
    }
};
class B{
public:
    void result(pGeneralFun fun, int a, int b){
        cout<<(*fun)(a, b)<

 

小结:

当类内的成员函数为静态成员函数时,将该函数作为回调函数,使用的方式与普通函数基本上没有区别。只是当接口函数定义在类的内部时,调用接口函数要使用对象名。

2、类的非静态成员函数作为回调函数

a、在类外定义接口函数

#include 
using namespace std;
class Test;                             // 在定义函数指针之前必须先声明类
typedef int (Test::*pClassFun)(int, int);                         // 定义函数指针
class Test{
public:
    int sum(int a, int b){
    return a+b;
    }
};
void result(Test *test, pClassFun fun, int a, int b){       // 在定义接口函数时要传入一个对象
    cout<<(test->*fun)(a, b)<

 

b、在类内定义接口函数

#include 
using namespace std;
class Test;
typedef int(Test::*pClassFun)(int, int);
class Test{
public:
    int sum(int a, int b){
        return a+b;
    }
    void result(pClassFun fun, int a, int b){
        cout<<(this->*fun)(a, b)<

 

c、在类 A 内定义回调函数,在类 B 内定义接口函数

方式一:

#include 
using namespace std;
class A;                  // 必须先声明类 A
typedef int(A::*pClassFun)(int, int);
class A{
public:
    int sum(int a, int b){
        return a+b;
    }
};
class B{
public:
    void result(A *test, pClassFun fun, int a, int b){
        cout<<(test->*fun)(a, b)<

 

方式二:

使用 stl 中的 function 和 bind

#include 
#include     // bind 的声明位于 头文件 functional 中
using namespace std;
using namespace std::placeholder;      // 占位符的定义位于命名空间 placeholder 中

typedef functionFun;         // 定义的是一个函数模板,指针类型

class B{
public:
    void call(int a, Fun f) {               // 在类 B 中定义接口函数
        cout<

你可能感兴趣的:(C++)