Could (Edit: Should) I do something like this?
Edit:
I'll try asking about an example that may be better suited to this inheritence scheme. Note this isn't a working class, just for the concept.
template<typename T>
class Tree {
protected:
class Node {
Node* _parent;
T _data
};
};
template<typename T>
class BinaryTree: public Tree {
private:
class BinaryNode: public Tree<T>::Node {
Node *_left, *_right;
};
};
This way of constructing parallel class hierarchies is not uncommon. However, it is more common to hide the nested derived class as an implementation detail that should be encapsulated, i.e. the
BiIterator
in your example would be in theprivate:
section.However,
Iterator
is not a good example because of object slicing: even ifBiIterator
remains public, this seemingly innocent code is incorrect:A better example would be if the base class member function took a reference or a pointer to an object of a nested class defined in the base, and a derived class would pass its derived nested class for it: