如何使 类的成员函数作为回调函数

借鉴:http://www.cnblogs.com/this-543273659/archive/2011/08/17/2143576.html
================================================FuncPointer.h=================================

#ifndef __H_FUNCPOINTER_H__
#define __H_FUNCPOINTER_H__


#include <iostream>
#include "cocos2d.h"


class FuncPoint
{
public:
FuncPoint();
~FuncPoint();
void createImg();
void init();


void testPoint(int x, int(*func)());
static int getNum();


void (*sp)();
static void spCopy1();
static void spCopy2();
protected:
private:
};
#endif




================================================FuncPointer.cpp=================================


#include "FuncPointer.h"


using namespace std;
USING_NS_CC;


FuncPoint::FuncPoint()
{
sp = spCopy1;
sp();
cocos2d::log("--------------------------good line-----------------------");
sp = spCopy2;
sp();
}
FuncPoint::~FuncPoint()
{


}




void FuncPoint::createImg()
{
testPoint(10, getNum);
sp = spCopy1;
sp();
}


void FuncPoint::testPoint(int x, int(*func)())
{
int i = func();
cocos2d::log("--------------------------good line--------------i = %d ---------",i);
}


 int FuncPoint::getNum()
{
int i = 100;
return i;
}




 void FuncPoint::spCopy1()
 {
cocos2d::log("-----------------------FuncPoint::spCopy1()-------------------");
 }
 void FuncPoint::spCopy2()
 {
cocos2d::log("----------------------FuncPoint::spCopy2()-------------------");
 }


================================================调用:=================================

#include"FuncPoint.h"


FuncPoint *sp = new FuncPoint();
sp->createImg();
delete sp;
    sp = nullptr;


================================================输出结果:=================================

-----------------------FuncPoint::spCopy1()-------------------

--------------------------good line-----------------------
----------------------FuncPoint::spCopy2()-------------------
--------------------------good line--------------i = 100 ---------
-----------------------FuncPoint::spCopy1()-------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值