cocos2d::Value
是许多基本类型(int,float,double,bool,unsigned char,char*和std::string)
还有std::vector<Value>
,std::unordered_map<std::string,Value>
和std::unordered_map<int,Value>
这些类的包装类型。
你可以将上面提及的基本类放进cocos2d::Value
对象将它们转换成对应的类型,反之亦然。
注意:cocos2d::Value不能像其它cocos2d类型一样使用retain/release和引用计数内存管理
示例Value val; // call the default constructorif (val.isNull()) { log("val is null");}else{ std::string str =val.getDescription(); log("The description of val0:%s",str.c_str());}//----------------------------------------------------Value val1(65); // initialize with a integer//Value val1(3.4f); // initialize with a float value//Value val1(3.5); // initialize with a double valuelog("The description of the integer value:%s",val1.getDescription().c_str());log("val1.asByte() = %c",val1.asByte());//----------------------------------------------------std::string strV = "string";Value val2(strV); // initialize with stringlog("The description of the string value:%s",val2.getDescription().c_str());//----------------------------------------------------auto sp0 = Sprite::create();Vector<Object*>* vecV = new Vector<Object*>();vecV->pushBack(sp0);Value val3(vecV); // initialize with Vectorlog("The description of the Vector value:%s",val3.getDescription().c_str());delete vecV;//----------------------------------------------------Map<std::string,Object*>* mapV = new Map<std::string,Object*>();mapV->insert(strV,sp0);Value val4(mapV); // initialize with Maplog("The description of the Map value:%s",val4.getDescription().c_str());delete mapV;//----------------------------------------------------Value val6(&val4); // initialize with Maplog("The description of the Value-type value:%s",val6.getDescription().c_str());//----------------------------------------------------val2 = val1; // assigning between 2 Value-typelog("operator-> The description of val2:%s",val2.getDescription().c_str());val2 = 4; //assigning directlylog("operator-> The description of val4:%s",val2.getDescription().c_str());@H_404_134@结果是:
cocos2d: val is null
cocos2d: The description of the integer value:
65cocos2d: val1.asByte() = A
cocos2d: The description of the string value:
stringcocos2d: The description of the Vector value:
truecocos2d: The description of the Map value:
truecocos2d: The description of the Value-type value:
truecocos2d: operator-> The description of val2:
65cocos2d: operator-> The description of val4:
最佳用法
4考虑使用cocos2d::Value和新的模版容器(cocos2d::Vector和cocos2d::Map)优于使用cocos2d::CCBool,cocos2d::CCfloat,cocos2d::CCDouble,cocos2d::CCString,cocos2d::CCInteger和旧的objective-c风格的容器(cocos2d::CCArray和cocos2d::CCDictionary)。
当要使用基本类型的聚合时,将基本类型包装成cocos2d::Value,然后将它们和模版容器cocos2d::Vector和cocos2d::Map联合使用。
总结以上是内存溢出为你收集整理的Cocos2d-x 3.0数据结构——cocos2d::Value全部内容,希望文章能够帮你解决Cocos2d-x 3.0数据结构——cocos2d::Value所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)