Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A couple of points:</p> <ul> <li>Use a single <code>std::vector&lt;T&gt;</code> instead of <code>std::vector&lt;std::vector&lt;T&gt;&gt;</code>. Index it with y*r+x - use and operator overload to make this easier (see next point). This will be more memory efficient and slightly faster (and your init will be a lot easier: <code>resize(r*c)</code>).</li> <li>Make your data members private to protect your matrix against unintentional size changes. For example, user code can currently resize the vectors but leave the old <code>r</code> and <code>c</code>.</li> <li>Overload <code>operator()</code> to access the matrix (both const and non-const). If you really must use the <code>matrix[r][c]</code> syntax instead of <code>matrix(r,c)</code>, consider overloading <code>operator[]</code> and returning an iterator to the correct row (vector iterators are random access, so they will provide <code>operator[]</code>).</li> <li>Implement <code>operator+</code> as a non-friend non-member function instead - Improves encapsulation!</li> <li>Use initialization lists as others suggested.</li> <li>Let the constructor that currently takes a <code>T*</code> take an iterator instead. That way you automagically get the pointers support along with a lot of other cool things, such as range checking for debug-iterators, automatic type conversion for compatible value types and support for all other random access iterators. Also consider filling your matrix row-wise, so you can use forward iterators too.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload