借鉴: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()-------------------