c++成员函数做回调函数

来源:互联网 发布:小米网络机顶盒多少钱 编辑:程序博客网 时间:2024/06/08 03:01
<pre name="code" class="plain">类成员函数做回调函数有如下两种方法:
1、成员函数是静态函数,但是静态函数只能访问类成员的静态变量和静态函数2、成员函数对用类对象,用对象调用赋值后的函数指针举例说明如下:


#include <iostream>#include <stdio.h>#include <string>using namespace std;class test{public:test(){}~test(){}public:void func(){printf("xxxxxxxxxxxxx\n");}static void func1(){printf("aaaaaaaaaaaaa\n");}};typedef void (test::*FUNC)();void print(test *a, FUNC p){(a->*p)();}int main(){test a;print(&a, &test::func);typedef void(*FUNC1)();FUNC1 b = &test::func1;b();system("pause");return 0;}


0 0
原创粉丝点击