I have the following problem. Consider the classes
class face {
virtual std::vector<ptr>& get_vertices(void) const = 0;
};
class triangle : public face {
private:
std::vector<ptr> vertices;
public:
std::vector<ptr>& get_vertices(void) const { return vertices; };
};
class quadrilateral : public face {
private:
std::vector<ptr> vertices;
public:
std::vector<ptr>& get_vertices(void) const { return vertices; };
};
Obviously, a triangle and quadrilateral will always have 3 and 4 vertices respectively.
Thus, I would like to exchange std::vector by std::array of appropriate size to save the overhead induced by the three additional pointers in std::vector. (This is because I will have millions of faces …) Now, is there a chance to have a common access function in face with std::array as with std::vector above?
With a standard C-array, I would simply return a pointer to the first array entry and its size. Is there a STL-way to do the same or something similar? Or is there any other nice way to achieve this functionality?
Thanks for reading and possibly answering!!
Andreas
std::arraysof different sizes are different types, so I cannot see any way to do what you want. But what if you return thebegin()andend()const iterators to whatever container you hold internally, or an small range object containing both? That way you decouple the size of the container from the interface, leaving it to the implementation.EDIT: Just to clarify, in order to hide the data storage representation (in this case, size of the
std::array), you will need your own iterator class forface. This is easy to implement in terms of pointers since for eachfacespecialization you know the size, begin and end of the underlying data structure. But you cannot directly use thestd::array‘sbegin()andend()in the interface, obviously.Example:
This is a quick and dirty example illustrating how to implement part of the forward iterator behaviour using pointers. I use the virtual base class and one of the implementations from OP as starting point. I have also omitted all constructors, assignment operators, etc. It also assumes a class Edge (presumably a 2D or 3D point?).
};