2011-02-23 78 views
0

解析yaml文件時,通常我們從解析器中獲得根節點。使用yaml-cpp解析YAML文件時,是否'複製'所有子節點?

我想知道如果我可以在解析過程後引用根節點。如下所示。

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

我可以使用上面的代碼嗎?

回答

1

是的,它確實 - 一旦Node被解析,它不依賴於從Parser任何記憶。

這就是說,在你的例子中,你從來沒有真正構造過指向globalRoot的節點。你需要調用

globalRoot = new YAML::Node; 

,甚至更好,把它放在一個智能指針像std::auto_ptr

+0

是的,我錯過了。感謝您的回答!我現在可以使用它;) – SeniorLee 2011-02-23 05:19:05