Value使用

来源:互联网 发布:利率敏感性缺口 数据 编辑:程序博客网 时间:2024/06/07 05:36

// 使用integer初始化Value

Value val1(65);

// 使用float初始化Value

//Value val1(3.4f);

// 使用double初始化Value

//Value val1(3.5);

log("The description of the integer value:%s",val1.getDescription().c_str());//整型转换为字符串型

log("val1.asByte() = %c",val1.asByte());//字节转化,结果为A

//----------------------------------------------------

std::string strV = "fkava";

// 使用string初始化Value

Value val2(strV);

log("The description of the string value:%s",val2.getDescription().c_str());

//----------------------------------------------------

auto sp0 = Sprite::create();

Vector<Ref*>* vecV = new Vector<Ref*>();

vecV->pushBack(sp0);

// 使用Vector初始化Value

Value val3(vecV);

log("The description of the Vector value:%s",val3.getDescription().c_str());

delete vecV;

//----------------------------------------------------

Map<std::string, Ref*>* mapV = new Map<std::string, Ref*>();

mapV->insert(strV,sp0);

// 使用Map初始化Value

Value val4(mapV);

log("The description of the Map value:%s",val4.getDescription().c_str());

delete mapV;

//----------------------------------------------------

// 使用Map初始化Value

Value val6(&val4);

log("The description of the Value-type value:%s",val6.getDescription().c_str());

//----------------------------------------------------

// val1赋值给val2

val2 = val1;

log("operator-> The description of val2:%s",val2.getDescription().c_str());

// 直接分配值

val2 = 4;

log("operator-> The description of val4:%s",val2.getDescription().c_str());

 

 

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 对象中,然后将它们转化为对应的原生类型。

 

Value val;   // 调用默认构造函数  

if (val.isNull()) {  

    log("val is null");  

}else{  

    std::string str =val.getDescription();  

    log("The description of val0:%s",str.c_str());  

}  

 

//----------------------------------------------------  

Value val1(65);   // 用一个 int初始化  

//Value val1(3.4f);   // 用一个 float初始化  

//Value val1(3.5);   // 用一个 double初始化  

log("The description of the integer value:%s",val1.getDescription().c_str());  

log("val1.asByte() = %c",val1.asByte());  

 

//----------------------------------------------------  

std::string strV = "string";  

Value val2(strV);   // string初始化  

log("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);   // Vector初始化  

log("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);   // Map初始化  

log("The description of the Map value:%s",val4.getDescription().c_str());  

delete mapV;  

 

//----------------------------------------------------  

Value val6(&val4);   // Map初始化  

log("The description of the Value-type value:%s",val6.getDescription().c_str());  

 

//----------------------------------------------------  

val2 = val1;   // 在两个不同指类型间赋值  

log("operator-> The description of val2:%s",val2.getDescription().c_str());  

val2 = 4;   // 直接赋值  

log("operator-> The description of val4:%s",val2.getDescription().c_str());  

输出:  

cocos2d: val is null  

cocos2d: The description of the integer value:  

65  

  

cocos2d: val1.asByte() = A  

cocos2d: The description of the string value:  

string  

 

cocos2d: The description of the Vector value:  

true  

  

cocos2d: The description of the Map value:  

true  

  

cocos2d: The description of the Value-type value:  

true  

  

cocos2d: operator-> The description of val2:  

65  

  

cocos2d: operator-> The description of val4:  

Value的作用和用法:在创建Value时,往构造函数里传入一个值,Value就会自动根据这个值来决定自己的类型。在获取Value的值时,就根据它的类型,调用as**函数获取。

整数、浮点型和字符串之间的转换

整型转为字符串: std::string str = "NO"+Value(1).asString();

字符串转为整型:log("%d",Value("1234").asInt())

浮点型转字符串:log("%s",Value(123.5f).asString().c_str())

字符串转浮点型:log("%f",Value("14.45").asFloat())