Let us consider the following OOP structure. The base class(interface) is the so called 'Node' class. Two other classes inherit from 'Node', namely 'ElementNode', 'TextNode'. I need to use a polymorphic container(vector>). At some point I need to get some class member from an element extracted via vector, which is hidden by the upcasting, for example text from 'TextNode'. My question is if, design wise, it is fine to keep adding virtual methods to the base class 'Node' in order to gain access to the hidden members. Another option would be to make use of down casting. Is the design flawed and if it is what alternative would you suggest?
Example code in C++:
class Node {
public:
Node() {};
virtual ~Node() {};
NodeTypes getNodeType() const { return nodeType; };
virtual std::vector<std::shared_ptr<Node>> &getChildren() = 0;
virtual void pushChild(std::shared_ptr<Node>) = 0;
private:
NodeTypes nodeType;
std::vector<std::shared_ptr<Node>> children;
};
class ElementNode : public Node {
public:
ElementNode(HtmlElements, Node*);
void setTag(HtmlElements);
//Inherited methods.
std::vector<std::shared_ptr<Node>> &getChildren();
void pushChild(std::shared_ptr<Node>);
Node* getFather() const;
};
class TextNode : public Node {
public:
TextNode(std::string);
std::string getText() const;
void setText(std::string);
//Inherited methods.
std::vector<std::shared_ptr<Node>> &getChildren();
void pushChild(std::shared_ptr<Node>);
Node* getFather() const;
private:
std::string text;
};