Hard
Composite file tree with parent back-pointers
What happens when this program runs?
main.cpp
#include <iostream>
#include <string>
#include <vector>
struct Node {
std::string name;
Node* parent = nullptr;
std::vector<Node> children;
explicit Node(std::string n, Node* p = nullptr) : name(std::move(n)), parent(p) {}
Node& add(std::string n) {
children.emplace_back(std::move(n), this);
return children.back();
}
std::string path() const {
return parent ? parent->path() + "/" + name : name;
}
};
int main() {
Node root("root");
Node& docs = root.add("docs");
Node& readme = docs.add("readme.md");
std::cout << readme.path() << "\n";
root.add("src");
std::cout << root.children[0].children[0].path() << "\n";
}38 of 44 in Design Patterns