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

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

2015-01-16 00:08 323 查看
借鉴: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()-------------------");

 }

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

#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()-------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++