<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<div class="moz-cite-prefix">On 5/5/2016 9:00 AM, Kurt Schwehr
wrote:<br>
</div>
<blockquote
cite="mid:CACmBxytc68Lj5ApBHswbmZ42nzzhNZLvB-yitzs87JjYPcyi8Q@mail.gmail.com"
type="cite">Thanks! I've integrated your derived class in the
alternates section and Even's response about commenting on resize
into the drawbacks line
<div><br>
</div>
<div>Can you provide (on the list would be best) a bit more on why
the vector suggestion makes the it less tempting to do pointer
tricks?</div>
</blockquote>
<br>
First I want to make clear that use of std::vector doesn't <i>prevent</i>
using pointer tricks. At least for me (and many others I've worked
with) the fact that something has been defined as a std::vector
creates a mental barrier to using an interface other than that
declared in std::vector. To a large degree the strength of this
barrier will reflect the programmer's feelings towards
object-orientation and all that goes with it. If you're an
old-school C programmer who thinks all this new C++ "O-O crap" is
for sissies, then the first thing you'll do is grab the address of
element zero and use all the nasty, obscure coding style you've
always used. (Are my prejudices showing? And for what it's worth, I
certainly qualify as old school by virtue of age, but like to think
I've learned something along the way.) If, on the other hand, you
actually program in C++ (as opposed to using the C++ compiler on C
code), you'll look at the object as a vector and use the interface.<br>
<br>
Here's what I mean by "pointer tricks" - <br>
<br>
Imagine you have to do some operations on the diagonal of a square
matrix. Consider the readability of the two snippets:<br>
<br>
<blockquote>// I'm assuming we defined our array as a vector of
vectors.<br>
for (int i = 0; i < matrix_size; ++i)<br>
{<br>
do something(the_matrix[i][i]); <br>
}<br>
</blockquote>
<br>
vs.<br>
<br>
<blockquote>//Since I would never do this myself, a "real C
programmer" would almost certainly write this differently, esp.
the test for the end of the loop, but you get the idea<br>
for (int * i = the_matrix; i < sizeof(the_matrix) +
the_matrix; i += matrix_size + 1))<br>
{<br>
do_something(i);<br>
}<br>
</blockquote>
<br>
naming the loop var as diag_element would help, but in the first
example it's clear that do_something() is operating on a diagonal
element. In the second example it's not clear what you're operating
on at all. <br>
<br>
</body>
</html>