inheritance - Should I inherit a nested class within a derived class in c++? -
could (edit: should) this?
edit:
i'll try asking example may better suited inheritence scheme. note isn't working class, 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 not uncommon. however, more common hide nested derived class implementation detail should encapsulated, i.e. biiterator
in example in private:
section.
however, iterator
not example because of object slicing: if biiterator
remains public, seemingly innocent code incorrect:
bidirectionallist bilist; // slice off biiterator's functionality iterator iter(bilist.get_iterator()); while (iter.has_next()) { ... }
a better example if base class member function took reference or pointer object of nested class defined in base, , derived class pass derived nested class it:
class task { public: class worker { public virtual void work()=0; } void run(worker& w); }; class specialtask : public task { private: class specialworker : public worker { public virtual void work() { ... } }; public: void do_something() { specialworker w; run(w); // passes specialworker run() of base class } };
Comments
Post a Comment