Note that there are some explanatory texts on larger screens.

plurals
  1. POSTLifying C++ classes
    text
    copied!<p>I'm trying to write a class which contains several std::vectors as data members, and provides a subset of vector's interface to access them:</p> <pre><code>class Mesh { public: private: std::vector&lt;Vector3&gt; positions; std::vector&lt;Vector3&gt; normals; // Several other members along the same lines }; </code></pre> <p>The main thing you can do with a mesh is add positions, normals and other stuff to it. In order to allow an STL-like way of accessing a Mesh (add from arrays, other containers, etc.), I'm toying with the idea of adding methods like this:</p> <pre><code>public: template&lt;class InIter&gt; void AddNormals(InIter first, InIter last); </code></pre> <p>Problem is, from what I understand of templates, these methods will have to be defined in the header file (seems to make sense; without a concrete iterator type, the compiler doesn't know how to generate object code for the obvious implementation of this method).</p> <ol> <li><p>Is this actually a problem? My gut reaction is not to go around sticking huge chunks of code in header files, but my C++ is a little rusty with not much STL experience outside toy examples, and I'm not sure what "acceptable" C++ coding practice is on this.</p></li> <li><p>Is there a better way to expose this functionality while retaining an STL-like generic programming flavour? One way would be something like this:</p></li> </ol> <p>(end list)</p> <pre><code>class RestrictedVector&lt;T&gt; { public: RestrictedVector(std::vector&lt;T&gt; wrapped) : wrapped(wrapped) {} template &lt;class InIter&gt; void Add(InIter first, InIter last) { std::copy(first, last, std::back_insert_iterator(wrapped)); } private: std::vector&lt;T&gt; wrapped; }; </code></pre> <p>and then expose instances of these on Mesh instead, but that's starting to reek a little of overengineering :P Any advice is greatly appreciated!</p>
 

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