加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos3.x之C++11新特性

发布时间:2020-12-14 20:17:13 所属栏目:百科 来源:网络整理
导读:http://www.jb51.cc/article/p-qmdcjoko-sw.html Cocos2dx是用C++11编写的,由于之前工作中并没有用到这部分。这里对C++11新特性在Cocos2dx中使用较多的地方做一下归纳。 C++11FAQ:http://www.stroustrup.com/C++11FAQ.html 一、新的关键字及语法 nullptr:

http://www.52php.cn/article/p-qmdcjoko-sw.html

Cocos2dx是用C++11编写的,由于之前工作中并没有用到这部分。这里对C++11新特性在Cocos2dx中使用较多的地方做一下归纳。

C++11FAQ:http://www.stroustrup.com/C++11FAQ.html

一、新的关键字及语法

nullptr:用来代替NULL,nullptr是强类型,防止出现一些二义性

[cpp] view plain copy
  1. voidf(int);//#1
  2. char*);//#2
  3. //C++03
  4. f(0);//二义性
  5. //C++11
  6. f(nullptr)//无二义性,调用f(char*)

auto:根据上下文自动类型推导,(在使用STL时非常方便),(decltype与此相反,从变量或表达式中获取类型)

    boolAppDelegate::applicationDidFinishLaunching(){
  1. //initializedirector
  2. autodirector=Director::getInstance();//Director*
  3. autoglview=director->getOpenGLView();//GLView*
  4. ...
  5. }

override:派生类重写基类的虚函数时,在函数的声明中加上override(非必须),这样可在编译时检测出对基类函数的错误重写

    structB{
  1. virtualvoidf();
  2. voidg()const;
  3. voidh(char);
  4. voidk();//notvirtual
  5. };
  6. structD:B{
  7. voidf()override;//OK:overridesB::f()
  8. voidg()override;//error:wrongtype
  9. char);//overridesB::h();likelywarning
  10. voidk()override;//error:B::k()isnotvirtual
  11. };

final:可用来修饰基类的虚函数,表示该函数不可被派生类重写即override

    voidf()constfinal;//donotoverride
  1. voidg();
  2. structD:B{
  3. const;//error:D::fattemptstooverridefinalB::f
  4. voidg();//OK
  5. };

range for:只要定义了begin(),end()即有iterator

    voidf(vector<double>&v)
  1. {
  2. for(autox:v)cout<<x<<'n';
  3. for(auto&x:v)++x;//usingareferencetoallowustochangethevalue
  4. }

lambad表达式:主要应用时标书某些具有简单行为的函数,(cocos2dxz中常用)

    autoonTouchEvent=[&](EventListener*l)->bool{//Returntruetobreak
  1. EventListenerTouchOneByOne*listener=static_cast<EventListenerTouchOneByOne*>(l);
  2. //Skipifthelistenerwasremoved.
  3. if(!listener->_isRegistered)
  4. returnfalse;
  5. event->setCurrentTarget(listener->_node);
  6. 二、标准库

    1.std::function与std::bind

    std::function :可以定义类似函数指针的类型

    std:bind:可以方便的绑定类的成员函数

    这个常在cocos2dx中的回调函数中使用

      std::function<void(conststd::vector<Touch*>&,Event*)>onTouchesBegan;
    1. std::function< std::function<

      //newcallbacksbasedonC++11
    1. #defineCC_CALLBACK_0(__selector__,__target__,...)std::bind(&__selector__,##__VA_ARGS__)
    2. #defineCC_CALLBACK_1(__selector__,std::placeholders::_1,##__VA_ARGS__)
    3. #defineCC_CALLBACK_2(__selector__,std::placeholders::_2,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18.461538314819336px; margin:0px!important; padding:0px 3px 0px 10px!important"> #defineCC_CALLBACK_3(__selector__,std::placeholders::_3,##__VA_ARGS__)

    2.std::thread

    Cocos2dx引擎的核心仍然是一个单线程的死循环(UI线程),在处理一些比较耗时的工作,如网络通信,纹理资源,音视频资源等,为防止界面出现卡顿,最好还是另开线程(Worker线程)。而在3.2的版本中并未发现pthread的支持,原来是C++11的标准库中已经有了std::thread。下面给出一个简单示例:

      boolHelloWorld::init()
    1. if(!Layer::init())
    2. false;
    3. }
    4. std::threadt1(&HelloWorld::myThread,this);//创建一个分支线程,回调到myThread函数里
    5. t1.join();
    6. //t1.detach();
    7. log("inmajorthread");//在主线程
    8. true;
    9. voidHelloWorld::myThread()
    10. {
    11. log("inmythread");
    12. C++11中还有很多其他的新特性,如右值引用与move语义,std::move,无序容器(unordered_map...),初始化列表等;想更深入的了解请查看c++11FAQ

      (编辑:李大同)

      【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读