Question about code re-use (Template or Inheritance)
I wrote a link list class and now besides the type of the cur object it can handle now.
I'm interested in reusing the code so it can handle another object.
I was thinking something maybe like this? Right now my current classes are truck and events and I have a linklist class. By using the linklist class it's right now setup for e开发者_如何学编程vents, but trucks will have the same kinda behavior in the linklist it will just be a few minors changes to the list. Instead of just copying it and reusing it like that. Could I setup both Truck and Events to be a Item? so Item is the base class and Truck and Events inherit from Item. SO that then I can pass in the truck and event into the list as items, would this work?
Or templatize the list?
A template is definitely the right choice. All of us who've been around since before templates were available can attest to their superiority over the alternatives.
Of course, unless you have some overwhelming reason to write your own, you're probably better off using std::list
(or, perhaps, Boost slist
).
精彩评论