STLifying C++ classes
- by shambulator
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:
class Mesh
{
public:
private:
std::vector<Vector3> positions;
std::vector<Vector3> normals;
// Several other members along the same lines
};
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:
public:
template<class InIter>
void AddNormals(InIter first, InIter last);
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).
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.
Is there a better way to expose this functionality while retaining an STL-like generic
programming flavour? One way would be something like this:
(end list)
class RestrictedVector<T>
{
public:
RestrictedVector(std::vector<T> wrapped)
: wrapped(wrapped) {}
template <class InIter>
void Add(InIter first, InIter last)
{
std::copy(first, last, std::back_insert_iterator(wrapped));
}
private:
std::vector<T> wrapped;
};
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!