C++类成员函数指针,typedef 类成员函数指针的例子。

//testDelegate.h
//rorger ,2012.3.22
//contact me:jhcyd112@163.com
#include "iostream"
using namespace std;

class DelegateProtocol{
public:
	static void StaticCalllFunc();
	static int staticData;

	virtual void callFunc();
	virtual int callFuncO(int i);
	virtual void callFuncN(void* data);
	int data;
};

//可以这样声明指向类成员函数的指针,静态的类成员函数和普通c函数一样。
/*void (DelegateProtocol::*pCallFunc)();
int (DelegateProtocol::*pCallFuncO)(int i);
void (DelegateProtocol::*pCallFuncN)(void* data);
void (*pStaticCallFunc)();*/
//typedef
typedef void (DelegateProtocol::*CALLFUNC_HANDLER)();
typedef int (DelegateProtocol::*CALLFUNCO_HANDLER)(int i);
typedef void (DelegateProtocol::*CALLFUNCN_HANDLER)(void *data);
typedef void (*CALLFUNCS_HANDLER)(); //静态函数定义类型和c语言普通函数一致,无需类前缀

#define  func_selector(_SELECTOR) (CALLFUNC_HANDLER)(&_SELECTOR)
#define  funco_selector(_SELECTOR) (CALLFUNCO_HANDLER)(&_SELECTOR)
#define  funcn_selector(_SELECTOR) (CALLFUNCN_HANDLER)(&_SELECTOR)
#define  funcs_selector(_SELECTOR) (CALLFUNCS_HANDLER)(&_SELECTOR)


class SubDelegate:public DelegateProtocol
{
public:
	static void StaticCalllFunc();
	static int staticData;
	virtual void callFunc();
	virtual int callFuncO(int i);
	virtual void callFuncN(void* data);
};

//testDelegate.cpp
//rorger ,2012.3.22
//contact me:jhcyd112@163.com
#include "testDelegate.h"

int DelegateProtocol::staticData=0;
int SubDelegate::staticData=5;
void  DelegateProtocol::StaticCalllFunc(){
	printf("This is DelegateProtol::StaticCallFunc\n");
	staticData++;
}

void DelegateProtocol::callFunc(){
	printf("This is DelegateProtol::callFunc\n");
}

int DelegateProtocol::callFuncO(int i){
	printf("This is DelegateProtol::callFuncO i:%d\n", i);
	return i*2;
}

void DelegateProtocol::callFuncN(void* data){
	printf("This is DelegateProtol::callFuncN dataAddress:0x%X\n",data);
}

void  SubDelegate::StaticCalllFunc(){
	printf("This is SubDelegate::StaticCallFunc\n");
	staticData++;
}


void SubDelegate::callFunc(){
	printf("This is SubDelegate::callFunc\n");
}

int SubDelegate::callFuncO(int i){
	printf("This is SubDelegate::callFuncO i:%d\n", i);
	return i*2;
}

void SubDelegate::callFuncN(void* data){
	printf("This is SubDelegate::callFuncN dataAddress:0x%X\n",data);
}

 


 

//rorger, 2012,3,22
//contact me:jhcyd112@163.com

#include "iostream"
#include "testDelegate.h"
using namespace std;

void testDelegateCallFunc(DelegateProtocol* delegate,CALLFUNC_HANDLER handler){
	(delegate->*handler)();
}
int testDelegateCallFuncO(DelegateProtocol* delegate,CALLFUNCO_HANDLER handler, int i){
	return (delegate->*handler)(i);
}
void testDelegateCallFuncN(DelegateProtocol* delegate,CALLFUNCN_HANDLER handler, void* data){
	(delegate->*handler)(data);
}
void testDelegateCallFuncS(DelegateProtocol* delegate,CALLFUNCS_HANDLER handler){
	handler();
}

int main(){
	int a=10;
	DelegateProtocol* protocol = new DelegateProtocol();
	testDelegateCallFunc(protocol,func_selector(DelegateProtocol::callFunc));
	a=testDelegateCallFuncO(protocol,funco_selector(DelegateProtocol::callFuncO),a);
	testDelegateCallFuncN(protocol,funcn_selector(DelegateProtocol::callFuncN),(void*)&a);
	testDelegateCallFuncS(protocol, funcs_selector(DelegateProtocol::StaticCalllFunc));

	SubDelegate* subDelegate = new SubDelegate();
	testDelegateCallFunc(subDelegate,func_selector(SubDelegate::callFunc));
	a=testDelegateCallFuncO(subDelegate,funco_selector(SubDelegate::callFuncO),a);
	testDelegateCallFuncN(subDelegate,funcn_selector(SubDelegate::callFuncN),(void*)&a);
	testDelegateCallFuncS(subDelegate, funcs_selector(SubDelegate::StaticCalllFunc));

	int  *number = &(SubDelegate::staticData);
	printf("number:%d\n",*number);
	system("pause");
	return 0;
}
/*
This is DelegateProtol::callFunc
This is DelegateProtol::callFuncO i:10
This is DelegateProtol::callFuncN dataAddress:0x12FEF8
This is DelegateProtol::StaticCallFunc
This is SubDelegate::callFunc
This is SubDelegate::callFuncO i:20
This is SubDelegate::callFuncN dataAddress:0x12FEF8
This is SubDelegate::StaticCallFunc
number:6
Press any key to continue . . .

*/

/*声明指向类成员函数的指针,可以看出结果是具有多态性的*/


 

 点此查看c语言回调函数的例子:

http://blog.csdn.net/rorger/article/details/7384969

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值