J
James Kanze
On Apr 3, 5:35 pm, James Kanze <[email protected]> wrote:
[...]
From the context it is hard (at least for me) to know what the
OPs base abstraction actually is.
Is, or should be? From his code, the base class was
std::vector<Point2D>, so that's the base abstraction. Whether
this is a good design decision is a legitimate question---I
rather think unwrapped standard containers are too widely used
myself. But his question is basically, given this design
decision, whether it is appropriate to additional functionality
that isn't in std::vector (but might be appropriate for
std::vector<Point2D>). And that having decided to do so, is the
member function syntax the appropriate way to do so.
[...]
However, since vector has been exposed as a public interface
clients are likely to take advantage of that. Ie they are
going to write code that works (or works well) only with
random access iterators, contiguous memory, constant time
size, etc.
That's always a problem when you use a standard container. He's
made the choice that his type will have random access iterators,
and will support insertion at the end (but not necessarily
elsewhere) in ammortized constant time.
See above. One example that comes to mind is assuming the
iterators are random access. Another more insidious one would
happen if he had chosen std::list for the first round and
then switched to std::vector.
He can't switch. Ever. But why would he think he could. You
can't normally modify a base class (except maybe to extend it);
what you expect to be able to switch is the derived class. The
base class defines your contract. By definition, it's not an
implementation detail.
As he presented the problem, he has guaranteed random access
iterators (for example). It may be that he's guaranteeing more
than he should---in general, the less you guarantee, the better.
But that's a separate question from the one he asked.
Note that using composition, then using a typedef for the
iterator, has the same problem. I have a couple of classes that
"wrap" the iterator precisely for this reason.