mardi 15 août 2017

How to prevent memory reallocation using std::vector

I've read a lot of question but no one answer me for my specific case.

Actually I have

std::vector<Point2Dd> points;
std::vector<Triangle> triangles;

Point2Dd is a class for a 2D point, it is not important to specify how it is implemented.

Triangle however is implemented like:

class Triangle{
    public:
     Triangle();
     Triangle(Point2Dd* p1, Point2Dd* p2, Point2Dd* p3);
     // Getter & setter

    private:
     Point2Dd* vA = nullptr;
     Point2Dd* vB = nullptr;
     Point2Dd* vC = nullptr;
}

that is, as three-pointers to vector of points.

Actually it work perfectly but I've think: if I add an other point into my vector and my vector change all memory address? All my triangles will be composed by invalid address.

I've read about using std::unique_ptr<Point2Dd> but I don't think is the best way.

Have you any solution? Thanks :)

Aucun commentaire:

Enregistrer un commentaire