使用"运算符[]"包装getter和setter

时间:2015-08-03 08:08:51

标签: c++ boost-propertytree

许多库为我们提供getter和setter来访问和修改已处理的变量。其中一个库是Boost PropertyTree。我想用boost::property_tree::ptreeoperator[]中包含getter和setter。

来源:

class XMLDocument {
    boost::property_tree::ptree ptree_;

public:
    XMLDocument(const std::string &content) {
        std::stringstream ss;
        boost::property_tree::read_xml(ss << content, ptree_);
    }
    template <typename T> T &operator[](const std::string &path) { /* TODO */ }
}

int main() {
    XMLDocument xml("<foo>bar</foo><baz>qux</baz>");
    std::cout << xml["foo"] << std::endl; // Access the element.
    xml["baz"] = "quux"; // Modify the element.
    std::cout << xml["baz"] << std::endl;
}

输出:

bar
quux

我该怎么办?

0 个答案:

没有答案