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

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