std::function 测试
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" typedef std::function<void(int code)> TestCallback; #define CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__) class HelloWorld : public cocos2d::Layer{ public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(HelloWorld); private: void handler(int code); void testCallback(TestCallback callback); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" USING_NS_CC; Scene* HelloWorld::createScene(){ auto scene = Scene::create(); auto layer = HelloWorld::create(); scene->addChild(layer); return scene; } bool HelloWorld::init(){ if ( !Layer::init() )return false; testCallback(CALLBACK_1(HelloWorld::handler,this)); return true; } void HelloWorld::handler(int code){ log("handler:%i",code); } void HelloWorld::testCallback(TestCallback callback){ callback(105); }
//输出:105