Fortunately, most C++ implementations provide a template
mechanism, enabling you to define generic header
and link
template classes.
As a step toward understanding C++'s template mechanism,
note that you can make the header
and link
classes
shown in Segment 763
look like templates by substituting a heavy black
bar, /
, for the railroad_car
string:
class link { friend class header; private: link *next_link_pointer; / *element_pointer; link (/ *e, link *l) { element_pointer = e; next_link_pointer = l; } }; class header { public: header ( ) { first_link_pointer = NULL; current_link_pointer = first_link_pointer; } void add (/ *new_element) { first_link_pointer = new link (new_element, first_link_pointer); current_link_pointer = first_link_pointer; } void advance ( ) { current_link_pointer = current_link_pointer -> next_link_pointer; } /* access ( ) { return current_link_pointer -> element_pointer; } int endp ( ) { return ! current_link_pointer; } void reset ( ) { current_link_pointer = first_link_pointer; } private: link *first_link_pointer; link *current_link_pointer; };