您的位置:首页 > 编程语言 > C语言/C++

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

2016-06-29 14:00 281 查看
<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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: