c++ 11基础 :
std::function
类模版std::function是一种通用、多态的函数封装。std::function的实例可以对任何可以调用的目标进行存储、复制、和调用操作,这些目标包括函数、lambda表达式、绑定表达式、以及其它函数对象等。
用法示例:
①保存自由函数
|
1
2
3
4
5
6
7
8
|
void
printA(
int
a)
{
cout<<a<<endl;
}
std::function<
(
a)>func;
func=printA;
func(2);
|
运行输出: 2
②保存lambda表达式
()>func_1=[](){cout<<
"helloworld"
<<endl;};
func_1();
运行输出:hello world
③保存成员函数
Foo{
Foo(
num):num_(num){}
print_add(
i)
const
{cout<<num_+i<<
'\n'
;}
num_;
};
//保存成员函数
(
Foo&,
)>f_add_display=&Foo::print_add;
Foofoo(2);
f_add_display(foo,1);
运行输出: 3
bind bind是一组用于函数绑定的模板。在对某个函数进行绑定时,可以指定部分参数或全部参数,也可以不指定任何参数,还可以调整各个参数间的顺序。对于未指定的参 数,可以使用占位符_1、_2、_3来表示。_1表示绑定后的函数的第1个参数,_2表示绑定后的函数的第2个参数,其他依次类推。
下面通过程序例子了解一下用法:
#include<iostream>
using
namespace
std;
class
A
{
public
:
fun_3(
k,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">m)
{
cout<<k<<
""
<<m<<endl;
}
};
fun(
x,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">y,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">z)
{
cout<<x<<
<<y<<
<<z<<endl;
}
fun_2(
&a,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">&b)
{
a++;
b++;
cout<<a<<
<<b<<endl;
}
main(
argc,
const
char
*argv[])
{
auto
f1=bind(fun,1,2,3);
//表示绑定函数fun的第一,二,三个参数值为:123
f1();
//print:123
f2=bind(fun,placeholders::_1,placeholders::_2,3);
//表示绑定函数fun的第三个参数为3,而fun的第一,二个参数分别有调用f2的第一,二个参数指定
f2(1,2);
//print:123
f3=bind(fun,3);
//表示绑定函数fun的第三个参数为3,而fun的第一,二个参数分别有调用f3的第二,一个参数指定
//注意:f2和f3的区别。
f3(1,0)!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">//print:213
n=2;
m=3;
f4=bind(fun_2,n,placeholders::_1);
f4(m);
//print:34
cout<<m<<endl;
//print:4说明:bind对于不事先绑定的参数,通过std::placeholders传递的参数是通过引用传递的
cout<<n<<endl;
//print:2说明:bind对于预先绑定的函数参数是通过值传递的
Aa;
f5=bind(&A::fun_3,a,placeholders::_2);
f5(10,20);
//print:1020
,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">)>fc=std::bind(&A::fun_3,std::placeholders::_1,std::placeholders::_2);
fc(10,0)!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">//print:1020
return
0;
}
|
CC_CALLBACK
一、通过 HelloWorldScene 中的 closeItem 开始
在cocos2d-x 2.x 版本中:
"Closenormal.png"
"CloseSelected.png"
this
menu_selector(HelloWorld::menuCloseCallback));
在cocos2d-x 3.0 版本中:
closeItem=MenuItemImage::create(
CC_CALLBACK_1(HelloWorld::menuCloseCallback,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">));
|
Director::getInstance()->end();
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
exit
(0);
#endif
注意到在3.0版本中使用到 CC_CALLBACK_1 这样一个宏定义。
5
//newcallbacksbasedonC++11
#defineCC_CALLBACK_0(__selector__,__target__,...)std::bind(&__selector__,##__VA_ARGS__)
#defineCC_CALLBACK_1(__selector__,##__VA_ARGS__)
#defineCC_CALCC_CALLBACK_1(HelloWorld::menuCloseCallback,this)LBACK_2(__selector__,std::placeholders::_2,##__VA_ARGS__)
#defineCC_CALLBACK_3(__selector__,std::placeholders::_3##__VA_ARGS__)
这里主要注意两点:一是std::bind,二是##_VA_ARGS_; ##_VA_ARGS_是可变参数宏
原来还有 CC_CALLBACK_0 1 2 3;而其中又有什么区别呢?
1、首先我们看看3.0版本中MenuItemImage的create方法:
1
MenuItemImage*MenuItemImage::create(
std::string&normalImage,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">std::string&selectedImage,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">ccmenuCallback&callback)
其中的回调参数是 ccmenuCallback
typedef
(Object*)>ccmenuCallback
来这里使用到了 C++ 中的 function 语法。
注意到 在CC_CALLBACK_ 的宏定义的中使用到的是 C++ 的 bind 语法,怎么不一致了呢?-- 见下面第四点 function
2、看回CC_CALLBACK_ 的宏定义
原来CC_CALLBACK_ 的宏定义中后面的 0 1 2 3分别表示的是 不事先指定回调函数参数的个数。
例如说CC_CALLBACK_ 1 表示的是,回调函数中不事先指定参数是一个,而事先指定的回调函数的参数 可以任意多个。
而且要注意到其中 不指定回调函数参数 和 指定回调函数参数 的顺序,注意不事先指定的在前,事先指定的在后。
下面通过例子说明这一点:
假设回调函数:
menuCloseCallback(Object*pSender,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">a,monospace!important; border:0px!important; bottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; width:auto!important; font-size:1em!important; min-height:inherit!important">b);
std::cout<<a<<
<<b<<std::endl;
Director::getInstance()->end();
#if(CC_TARGET_PLATFORM==CC_PLATFORM_IOS)
(0);
#endif
注意到在回调函数中输出 a b
4
ottom:auto!important; float:none!important; height:auto!important; left:auto!important; line-height:1.1em!important; outline:0px!important; overflow:visible!important; position:static!important; right:auto!important; top:auto!important; vertical-align:baseline!important; font-family:Consolas,2));
注意中其中 指定了两个参数 1 2
运行,在 点击closeItem 的时候,就会输出这两个事先指定的参数 1 2。
那么,不事先指定的参数是在什么时候传入的呢?
17
MenuItem::activate()
if
(_enabled)
{
(_callback)
{
_callback(
);
}
(kScriptTypeNone!=_scriptType)
{
BasicScriptDatadata(
);
ScriptEventscriptEvent(kMenuClickedEvent,&data);
ScriptEngineManager::getInstance()->getScriptEngine()->sendEvent(&scriptEvent);
}
}
注意到其中的 _callback(this); 对了,这个时候就传入了 这个不事先指定的回调函数参数。
这样,closeItem 的回调函数的void HelloWorld::menuCloseCallback(Object* pSender,int a,int b) 的三个参数都知道了。
第一个 不事先指定,在menu item调用 activate 的时候,_callback(this) 传入,this 也即是这个 menu item;第二、三个参数是事先指定的 1,2。
已经知道CC_CALLBACK_ 的宏定义是 std::bind 那么我们可以直接使用std::bind。
如下:
4
std::bind(&HelloWorld::menuCloseCallback,2));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。
相关推荐
如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2 1 4
本文实践自 RayWenderlich、Ali Hafizji 的文章《How To Create Dynamic Textures with CCRenderTexture in Cocos2D 2.X》,文中使用Cocos2D,我在这里使用Cocos2D-x 2.1.4进行学习和移植。在这篇文章,将会学习到如何创建实时纹理、如何用Gimp创建无缝拼接纹
Cocos-code-ide使用入门学习
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@163.com微信公众号:HopToad 欢迎转载,转载标注出处:http://blog.csdn.netotbaron/article/details/424343991. 软件准备 下载地址:http://cn.cocos2d-x.org/download 2. 简介2.1 引用C
Cocos2D-x-3.0 编译(Win7)
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从Cocos2D-x官网上下载,进入网页http://www.cocos2d-x.org/download,点击Cocos2d-x以下的Download v3.0,保存到自定义的文件夹2:从python官网上下载。进入网页https://www.python.org/downloads/,我当前下载的是3.4.0(当前最新
quick-cocos2d-x实例之挑战记忆极限设计文档
1. 来源 QuickV3sample项目中的2048样例游戏,以及最近《最强大脑》娱乐节目。将2048改造成一款挑战玩家对数字记忆的小游戏。邮箱:appdevzw@163.com微信公众号:HopToadAPK下载地址:http://download.csdn.net/detailotbaron/8446223源码下载地址:http://download.csdn.net/
Cocos2d-x 3 X CMake MinGW版本编译运行
Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试以QtCreatorIDE来进行CMake构建。Cocos2d-x3.X地址:https://github.com/cocos2d/cocos2d-x1.打开QtCreator,菜单栏→"打开文件或项目...",打开cocos2d-x目录下的CMakeLists.txt文件;2.弹出CMake向导,如下图所示:设置
vs 2013 编译cocos2d-x-3.9
下载地址:链接:https://pan.baidu.com/s/1IkQsMU6NoERAAQLcCUMcXQ提取码:p1pb下载完成后,解压进入build目录使用vs2013打开工程设置平台工具集,打开设置界面设置: 点击开始编译等待编译结束编译成功在build文件下会出现一个新文件夹Debug.win32,里面就是编译
Cocos2d-x CCControlPotentiometer之圆形音量button及特效
1. 圆形音量button事实上作者的本意应该是叫做“电位计button”。可是我觉得它和我们的圆形音量button非常像,所以就这么叫它吧~先看效果:好了,不多解释,本篇到此为止。(旁白: 噗。就这样结束了?)啊才怪~我们来看看代码:[cpp] viewplaincopyprint?CCContro
Cocos2d-x入门教程二简单的静态显示对象
原文链接:http://www.cnblogs.com/physwf/archive/2013/04/26/3043912.html为了进一步深入学习贯彻Cocos2d,我们将自己写一个场景类,但我们不会走的太远,凡是都要循序渐进,哪怕只前进一点点,那也至少是前进了,总比贪多嚼不烂一头雾水的好。在上一节中我们建
- • 如何使用CCRenderTexture创建动态纹理 …
- • Cocos-code-ide使用入门学习
- • Cocos2D-x-3.0 编译(Win7)
- • Cocos2d-x 2 0 在Windows平台下的使用
- • quick-cocos2d-x实例之挑战记忆极限设计…
- • Cocos2d-x 3 X CMake MinGW版本编译运行
- • vs 2013 编译cocos2d-x-3.9
- • cocos2d-x游戏开发系列教程-超级玛丽01…
- • Cocos2d-x CCControlPotentiometer之圆…
- • Cocos2d-x入门教程二简单的静态显示对象
- • cocos2d-x中CCScale9Sprite的另一种实现
- • Cocos2d-x v2.2.2版本+Win7+VS2010环境…
- • Ubuntu14.04+eclipse下cocos2d-x3.0正式…
- • 分别基于WIN32 API界面编程和Cocos2d-x…
- • Cocos2d-x 开发小记二:控件
- • Cocos2d-x 开发小记一:基本动作
- • 买Cocos2d-x视频课程送纸质图书
- • Cocos2d-x 学习笔记(11.10) Spawn