6

Seems Document can also be used as parameter in

void test(Value value);

and both Document and Value can have child value, what is the difference between them?

ggrr
  • 7,737
  • 5
  • 31
  • 53

1 Answers1

14

Firstly, the test function should not compile because Value does not support copy constructor. So you must use Value& value or const Value& value) instead.

Back to the question, Value represents a node in the DOM. Document derives from Value, and it represents the root of the DOM. Document provides functionality for parsing a JSON into the DOM, while Value cannot.

If the function does not need to call APIs dedicated for Document, such as Document::Parse(), you should use Value&. Passing a Document object to Value& parameter is OK in C++ too.

Milo Yip
  • 4,902
  • 2
  • 25
  • 27
  • Could you pls explain what do you mean by saying should use `Value&`, is there any shortage if I use `Document` instead of `Value`? – Roy Huang Jan 03 '21 at 12:59