Link to std :: an array of different sizes instead of std :: array in the base class

I have the following problem. Let's consider 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 a quadrangle will always have 3 and 4 vertices, respectively. Thus, I would like to exchange std :: vector for std :: array of the appropriate size in order to save the overhead caused by three extra pointers in std :: vector. (This is because I will have millions of faces ...) Now, is it possible to have a common access function in the face with std :: array, as with std :: vector above? With a standard C-array, I would just return a pointer to the first record of the array and its size. Is there an STL way to do the same or something similar? Or is there another good way to achieve this functionality?

, , ! Andreas

+3
2

std::arrays - , , . , begin() end() const , , , ? , .

EDIT. , ( std::array), face. , face , . std::array begin() end() .

:

, , . OP . , .. Edge ( ?).

class face {

public:

  typedef Edge* iterator;
  typedef const Edge* const_iterator;

  virtual iterator begin() = 0;
  virtual const_iterator begin() const = 0;
  virtual iterator end() = 0;
  virtual const_iterator end() const = 0;
  virtual size_t size() const = 0;

  virtual ~face() {};

};

class triangle : public virtual face {

public :
  virtual iterator begin() {return m_edges.begin();}
  virtual const_iterator begin() const {return m_edges.begin();}
  virtual iterator end() {return m_edges.end();}
  virtual const_iterator end() const {return m_edges.end();}
  virtual size_t size() const {return m_edges.size();}

private:
    std::array<Edge, 3> m_edges;

};

+6

std::array , get_vertices , ( ) . std::array, std::array, , face , :

template<size_t N>
class face {
    virtual std::array<ptr, N>& get_vertices(void) const = 0;
};
class triangle : public face<3>{
    //...
    std::array<ptr, 3>& get_vertices(void) const { return vertices; };
};
class quadrilateral : public face<4> {
    //...
    std::array<ptr, 4>& get_vertices(void) const { return vertices; };
};

, triangle quadrilateral : face<3> face<4> - . , triangle quadrilateral , , , get_vertices , . .

, :

class triangle : public face {
private:
    std::vector<ptr> vertices;
public:
    triangle() 
    {
           //it ensures that you've a vector of size 3, no more no less 
           vertices.reserve(3); 
    }
    std::vector<ptr>& get_vertices(void) const { return vertices; };
};

, quadrilateral:

    quadrilateral() 
    {
           //it ensures that you've a vector of size 4, no more no less 
           vertices.reserve(4); 
    }

, , , reserve(), , . .

+1

All Articles