When parsing yaml file, normally we get root node from parser.
And I'm wondering if I can reference the root node after parsing process. Like below.
YAML::Node* globalRoot;
void ParseDocument(filename)
{
YAML::Parser parser(fin)
parser.GetNextDocument(*globalRoot);
}
void myFunction()
{
ParseDocument("myYAML.yml");
// After the method above, we lose the parser instance since it's a local variable.
// But if all child data is copied, below code should be safe.
// If globalRoot is just pointing inside the parser, this could be dangerous.
std::string stringKey;
(*globalRoot)["myKey"] >> stringKey;
}
Can I use like code above??
Yes, it does - once a
Node
is parsed, it does not rely on any memory from theParser
.That said, in your example, you never actually construct the node pointed to by
globalRoot
. You'd need to calland even better, keep it in a smart pointer like
std::auto_ptr
.