According to http://www.cocos2d-x.org/wiki/Value,
Value can handle strings as well as int, float, bool, etc.
I'm confused when I have to make a choice between using
std::string
or
Value
In what circumstances should I use Value over std::string, and vice versa??
I think you have misunderstood the
Valueobject. As written in the documentation you linked to:So really
Valueis an object that wraps a bunch of other types of variables, which allows cocos2d-x to have loosely-typed structures like theValueMap(a hash of strings toValues- where eachValuecan be a different type of object) andValueVector(a list ofValues).For example, if you wanted to have a configuration hash with keys that are all strings, but with a bunch of different values - in vanilla C++, you would have to create a separate data structure for each type of value you want to save, but with
Valueyou can just do:It's just a mechanism to create some loose typing in C++ which is a strongly-typed language.
You can save a string in a Value with something like this:
And then later retrieve it with:
If you attempt to parse a
Valueas the wrong type, it will throw an error in runtime, since this is isn't something that the compiler can figure out.Do let me know if you have any questions.