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

引言:

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

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

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

例:

#include <iostream>
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)<<endl;
}
void main(){
    result(sum, 1, 2);                // 普通函数的函数名即为指针
}

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

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

a、在类外定义接口函数

#include <iostream>
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)<<endl;
}
void main(){
    result(Test::Max, 3, 4);           // 类的静态成员函数,类名::函数名 表示函数的地址
}

b、在类内定义接口函数

#include <iostream>
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)<<endl;
    }
};
void main(){
    Test test;
    test.result(Test::Max, 5, 6);           // 调用在类内定义的接口函数
}

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

#include <iostream>
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)<<endl;
    }
};
void main(){
    B b;
    A a;
    b.result(A::Max, 4, 5);
}

小结:

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

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

a、在类外定义接口函数

#include <iostream>
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)<<endl;
}
void main(){
    Test test;
    result(&test, &Test::sum, 3, 4);               // 对于非静态成员,要使用&来创建指向成员的指针
}

b、在类内定义接口函数

#include <iostream>
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)<<endl;               // 必须要用隐含的 this 指针来指向传入的函数
    }
};
void main(){
    Test test;
    test.result(&Test::sum, 3, 4);
}

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

方式一:

#include <iostream>
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)<<endl;
    }
};
void main(){
    A a;
    B b;
    b.result(&a, &A::sum, 2, 3);
}

方式二:

使用 stl 中的 function 和 bind

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

typedef function<int(int, int)>Fun;         // 定义的是一个函数模板,指针类型

class B{
public:
    void call(int a, Fun f) {               // 在类 B 中定义接口函数
        cout<<f(a, 2)<<endl;
    }
};
class A{
public:
    int sum(int a, int b){
        return a+b;
    }
    void bind(){
        Fun fun = std::bind(&A::sum, this, _1, _2);    
        // 其中_1,_2为占位符,通过bind函数将this指针隐式结合在子函数的参数中
        // 这一步只能在类 A 中实现,因为this指针指的是由类A产生的对象
        B b;
        b.call(1, fun);               // 在类 A 中调用 B 中的接口函数
        // 调用接口函数的这一步如果要在主函数中实现,则可以使bind函数返回 fun,在主函数中
        // 使用b.call(1, a.bind())来进行调用
    }
};
int main(){
    A a;
    B b;
    a.bind();
    system("pause");
}

猜你喜欢

转载自blog.csdn.net/xhj_enen/article/details/83789038