|
-
June 26th, 2005, 07:15 AM
#10
Re: extending an STL container class Question
I'm in complete agreement with Yves here. The whole design of the STL is based around extending functionality via non-member functions and decoupling the algorithm from the container. Inheriting subverts that design, increases coupling and decreases the cohesion of your class. There is no need to inherit from a container just to add a function. You add a non-member that takes iterators, and you have a decoupled solution that is extendible and modifiable. It the OP later decides that list<> would have been a better choice of container, the decoupled solution is trivial to change, whilst the inherited solution can be a real pain.
There's also the advice given in Sutter and Alexandrescu's C++ coding guidelines: inherit to be reused, not to reuse. I don't have the book with me at the moment, but I'll check it out tomorrow and expand on that. But for now, they are saying, in essence, that, yes, public inheritance means IS-A, and further, that IS-A implies polymorphism.
Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.
-- Sutter and Alexandrescu, C++ Coding Standards
Programs must be written for people to read, and only incidentally for machines to execute.
-- Harold Abelson and Gerald Jay Sussman
The cheapest, fastest and most reliable components of a computer system are those that aren't there.
-- Gordon Bell
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|