Re: inheriting from std::vector bad practice?
On Apr 4, 12:31 pm, Christian Hackl <ha...@sbox.tugraz.at> wrote:
Steve Chow ha scritto:
Originally I had a bunch of related functions that all took a vector
of Point2D as their argument.
Point2D findGreatestDistance(std::vector<Point2D>& points);
[...]
So in review:
[...]
c.) free floating + generic (so it can take anything with an
iterator?)
Or, as was suggested to you, you could make the calculation of
the distance between two points generic as well, which results
in two template parameters (one for the two iterators, another
one for the distance functor). That's in the spirit of the
algorithms provided by the standard library.
Unless there's some need for making it generic, that's just
extra complication. What does make sense *is* to write the
function to take iterators, rather than the container, since 1)
this will make making it generic easier, if the need later
arises, 2) this will allow calculating the greatest distance
over subpaths, without having the create a new array, and 3)
this is simply more idiomatic C++. In practice, it's mainly the
third reason, and one could argue that this isn't really a good
idiom, even if it's ubiquous. But that's the way things are.
(PS: how is it that someone posting from an Austrian address,
with a good Austrian family name like Hackl, has his system set
up to respond in Italian:-)?)
--
James Kanze