I have 3 classes. In it's simplest form, it looks like,
class tree
{
public:
tree_node* find_node(const std::string& text) {
return factory.find(text);
}
private:
tree_node_factory factory;
}
class tree_node
{
public:
tree_node(const std::string& text) : text_(text) {}
const std::string& text() const {
return text_;
}
void set_parent(const tree_node* new_parent);
private:
std::string text_;
}
class tree_node_factory
{
public:
tree_node* find(const std::string& text);
private:
std::vector<tree_node*> allocated_nodes;
}
I don't want to allow users of tree
to modify the tree_node
returned by methods like find_node
. So I changed, find_node
and tree_node_factory::find
to,
const tree_node* find_node(const std::string& text) const {
return factory.find(text);
}
const tree_node* find(const std::string& text) const;
Problem is tree
internally should be able to modify the nodes and work on methods like set_parent
. But since factory returns only const
nodes, I ended up with adding another overload (non const version) of find
into the factory.
tree_node* find(const std::string& text);
I am wondering is this the correct way to handle these kind of problems? I see the code is getting duplicated in the const and non-const versions.
Any thoughts..?