Cocos2dx3.2学习准备(一):C++11新特性

来源:互联网 发布:马列维奇 知乎 编辑:程序博客网 时间:2024/06/03 20:38

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

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

一、新的关键字及语法

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

void f(int); //#1void f(char *);//#2//C++03f(0); //二义性//C++11f(nullptr) //无二义性,调用f(char*)

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

bool AppDelegate::applicationDidFinishLaunching() {// initialize directorauto director = Director::getInstance();              // Director*auto glview = director->getOpenGLView();              // GLView*        ...}

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

struct B {virtual void f();virtual void g() const;virtual void h(char);void k();         // not virtual};struct D : B {        void f() override;// OK: overrides B::f()void g() override;// error: wrong typevirtual void h(char);// overrides B::h(); likely warningvoid k() override;// error: B::k() is not virtual};

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

struct B {virtual void f() const final;// do not overridevirtual void g();};struct D : B {void f() const; // error: D::f attempts to override final B::fvoid g();// OK};

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

void f(vector<double>& v){for (auto x : v) cout << x << '\n';for (auto& x : v) ++x;// using a reference to allow us to change the value}

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

auto onTouchEvent = [&](EventListener* l) -> bool { // Return true to break                EventListenerTouchOneByOne* listener = static_cast<EventListenerTouchOneByOne*>(l);                                // Skip if the listener was removed.                if (!listener->_isRegistered)                    return false;                             event->setCurrentTarget(listener->_node);                 ...}

二、标准库

1.std::function与std::bind

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

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

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

    std::function<void(const std::vector<Touch*>&, Event*)> onTouchesBegan;    std::function<void(const std::vector<Touch*>&, Event*)> onTouchesMoved;    std::function<void(const std::vector<Touch*>&, Event*)> onTouchesEnded;    std::function<void(const std::vector<Touch*>&, Event*)> onTouchesCancelled;

// 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__)

2.std::thread

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

bool HelloWorld::init(){  if ( !Layer::init() )  {    return false;  }    std::thread t1(&HelloWorld::myThread,this);//创建一个分支线程,回调到myThread函数里  t1.join();//t1.detach();  log("in major thread");//在主线程  return true;}void HelloWorld::myThread(){  log("in my thread");}

C++11中还有很多其他的新特性,如右值引用与move语义,std::move,无序容器(unordered_map...), 初始化列表等;想更深入的了解请查看c++11FAQ

如有错误欢迎指出,希望与Cocos2dx爱好者们多多交流


1 0
原创粉丝点击