zoukankan      html  css  js  c++  java
  • cocos进阶教程(5)CC_CALLBACK_X系列的使用技巧

    CC_CALLBACK_1,CC_CALLBACK_2,CC_CALLBACK_3

    这些都是std::bind的宏,数字1,2,3主要表示要占位的数量,也是将来传递参数的数量。

    // new callbacks based on C++11
    #define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
    #define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
    #define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
    #define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3 ##__VA_ARGS__)

    对应的std::bind有几个占位符std::placeholders::_1,std::placeholders::_2,std::placeholders::_3等,这些占位符才是我们在调用具体函数(&__selector__)的时候需要传入的参数,说白了就是告诉我们目标&__selector__的前面多少位需要传递参数的,CC_CALLBACK_2(__selector__,__target__, ...)这里的...也是可传入参数,但是这个参数不能取代占位参数的,占位参数只可以在调用的传递过去,这里的参数是站位参数以后的参数 给赋值个默认值而已。

    举例说明

    HelloWorld::sayName(name1, name2, name3, name4, name5){

    }

    CC_CALLBACK_3(self::sayName, self, "小明", "小丽") 这句话就是std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,"小明","小丽")

    假设

    auto delegateFucName = std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3,"小明","小丽")

    delegateFucName(...)这个时候...就是要传递的3个占位符的值,delegateFucName("小雪","小风","小花")

    最终就是占位符传递参数("小雪","小风","小花")+CC_CALLBACK_3后续参数("小明","小丽")完成目标函数sayName的参数构成

  • 相关阅读:
    php 服务器部署 500错误
    myeclipse 安装phpeclipse插件
    mysql修改引擎
    linux ftp命令
    jquery datepicker使用
    写一个函数代替php自带的include_once
    mysql 数据类型
    jquery option:last各浏览器支持不是很好
    html编辑器 学习
    今天去康盛面试,好歹我也工作3年了,还说我是初级选手,KAO
  • 原文地址:https://www.cnblogs.com/damowang/p/5020210.html
Copyright © 2011-2022 走看看