JsonCpp第五课 Json::Value使用技巧

1 对Json::Value的等号赋值都会引起原有值的变化,最终调用std::swap对值进行改变

Value& Value::operator=(const Value& other) {
  swap(const_cast<Value&>(other));
  return *this;
}

void Value::swapPayload(Value& other) {
  ValueType temp = type_;
  type_ = other.type_;
  other.type_ = temp;
  std::swap(value_, other.value_);
  int temp2 = allocated_;
  allocated_ = other.allocated_;
  other.allocated_ = temp2 & 0x1;
}

所以当如下代码执行的时候,将会node将会是空的

Json::Value node;

root["node"] = node;

2 Json::Value的拷贝性能

Json::Value node = root[i];

这个函数方式将会对数据进行拷贝,影响性能,因此正确的做法是

Json::Value& node = root[i];

上一篇:python-集合类型set与frozenset(操作符、方法、内置函数)


下一篇:安装Idea后需要做的3件事