"Leigh Johnston" <leigh@i42.co.uk> wrote in message
news:L6udnXNyLeWrwCrWnZ2dnUVZ8uednZ2d@giganews.com...
"Steve Chow" <robertogoberto@gmail.com> wrote in message
news:17d61f66-0685-42e3-9b20-5103cb58f12c@r1g2000yqj.googlegroups.com...
Originally I had a bunch of related functions that all took a vector
of Point2D as their argument.
Point2D findGreatestDistance(std::vector<Point2D>& points);
However, this didn't strike me as a very C++/OO way to do things, so I
found a solution I was happy with in:
class Path : public std::vector<Point2D>
{
public:
Path();
~Path();
Point2D findGreatestDistance();
/* related functions */
};
And it works, at least as far as I can tell. Yet it's been received by
people more knowledgeable than me as disgusting and wrong, without
explaining why. Is there a better way I should be doing this?
Someone suggested moving findGreatestDistance into Point2D (struct
with x,y and overload ==) but I don't see how that's possible because
it'd only be able to look at itself.
No in general it is not bad practice, Stroustrup does it in The C++
Programming Language (25.6.1 Adjusting Interfaces). The only thing you
have to watch out for is that a standard container's destructor is not
virtual.
See http://www.i42.co.uk/stuff/mutable_set.htm also (something I wrote
but deriving from map/multimap instead of vector).
The only time it is unwise to use public inheritance is if your class
invariant consists of more than just vector's invariant in which case it
might be possible to break your class's invariant by calling the vector's
member functions, but I don't believe this is the case in your example
(i.e. you are simply performing interface augmentation).
Sorry, that's bullshit. Proper design involves much more. It's possible to
discussion.