Value即是平常常用的所有数据的集合,用于数据的转换十分方便。
Value可以包含的数据包括三类
- 内置类型(int, bool, float, double, unsigned char)
- 字符串(const char*, std::string)
- 数组,字典(std::vector, std::unordered_map)
Value中包含这些类型的构造函数,并且为之重载了相应的=
运算符。
在这里仅列出成员变量:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| union { unsigned char byteVal; int intVal; float floatVal; double doubleVal; bool boolVal; }_baseData;
std::string _strData; ValueVector* _vectorData;
ValueMap* _mapData; ValueMapIntKey* _intKeyMapData;
|
用法
Value中有一系列asXxxxx()
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| unsigned char asByte() const; int asInt() const; float asFloat() const; double asDouble() const; bool asBool() const; std::string asString() const;
ValueVector& asValueVector(); const ValueVector& asValueVector() const;
ValueMap& asValueMap(); const ValueMap& asValueMap() const;
ValueMapIntKey& asIntKeyMap(); const ValueMapIntKey& asIntKeyMap() const;
|
所以,我们在cpp的代码中可以这样使用:
总结
纠结右值引用
好长一段时间
附上自己写的一些例子 =- =
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <iostream>
using namespace std;
class ec { public: ec() { cout << "ec()" << endl; }
~ec() { cout << "~ec()" << endl; }
ec(const ec & other) { cout << "ec(const ewc &other)" << endl; }
ec& operator = (const ec & other) { cout << " = (const ec & other)" << endl; return *this; }
ec& operator = (ec&& other) { cout << " = (ec && other)" << endl; return *this; } };
int main() { ec a = ec(); cout << "-----" << endl;
ec b = ec(a); cout << "-----" << endl;
a = b; cout << "-----" << endl;
b = ec(); cout << "-----" << endl;
return 0; }
|
还有个很有趣的例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| #include <iostream>
using namespace std;
class empty { public: empty() { cout << "empty()" << endl; a = rand(); }
~empty() { cout << "~empty()" << endl; }
void print() { cout << a << endl; }
public: int a; };
int main() { srand((unsigned int)time(nullptr));
empty ea; ea.print(); (empty() = ea).print(); (empty() = empty()).print();
return 0; }
|