您当前的位置: 首页 >  c++

phymat.nico

暂无认证

  • 0浏览

    0关注

    1967博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

c++ Boost库之boost::bind学习

phymat.nico 发布时间:2018-01-01 04:55:41 ,浏览量:0

  刚开始学c++,就看boost库其实有点小小的不情愿。 团队要求必掌握的Boost库: boost::bind boost::function boost::Signals2

  学习前奏:在写关于cocos2d-x的helloworld例子时,一直搞不明白这句代码的意思(以前搞java的)

1 void UpdateGame(cocos2d::ccTime time);
2 void CollideDetect(cocos2d::ccTime time);    
3     
4 this->schedule( schedule_selector(HelloWorld::UpdateGame), 0.05f );
5 this->schedule( schedule_selector(HelloWorld::CollideDetect) );

最近知道原来方法也是可以做为一种类型传其它函数。把参数定义为类型

复制代码
 1 typedef void (*FUN_TYPE) (int);
 2 
 3 #define  schedule_selector FUN_TYPE
 4 
 5 void  schedule( FUN_TYPE f, int i) {
 6     f(i);
 7 }
 8 
 9 
10 void  UpdateGame(int i) {
11     printf("hello %d", i);
12 }
13 
14 
15 int _tmain(int argc, _TCHAR* argv[])
16 {
17     schedule( schedule_selector(UpdateGame), 1);
18     return 0;
19 }
复制代码

绕了半天做了个行似的。为什么在cocos2d-x要这样做呢?抽象时,引擎制作者并不知道每一帧动画,或没隔x时间,执行一下你的UpdateGame()方法,由于你起名字不一定起UpdateGame()而且不一定有几个那样的方法(可以重写一下UpdateGame()使他执行你想关联的方法,但是执行的时间也不一定相同所以操作复杂度大大增加),所以直接定义成方法类型。

  从boost::bind开始学习Boost库:boost::bind is a generalization of the standard functions std::bind1st and std::bind2nd. It supports arbitrary function objects, functions, function pointers, and member function pointers, and is able to bind any argument to a specific value or route input arguments into arbitrary positions.译:boost::bind是标准方法std::bind1st&std::bind2nd的一般化。它支持任意的方法对象s,方法s,方法s的指针,和成员方法指针s,它可以给任何参数绑定一个指定的值,或者发送参数放到任何位置。比较痛苦的事,现在我还没看过,std::bind1st&std::bind2nd。我感觉自己有点误入歧途了,虽然刚刚掌握函数类型,但这肯定不是问题的关键。

复制代码
 1 class funtest {
 2 public:
 3     string str;
 4 public:
 5     void doSth(char c) {
 6         cout             
关注
打赏
1659628745
查看更多评论
立即登录/注册

微信扫码登录

0.3618s