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];

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章