1
votes

Consider 2 variables (number of polygons and its coordinates) :

int numberPoly= 2;
float polycoord[18]= {0,50,0,0,0,0,50,0,0,0,50,0,50,50,0,50,0,0};

, a Model class (that is intended to store polygon classes to a list) :

class model{
    public:
        model();
        void affect(int id, int address){
            polyclasses[id]=address;
        }
    private:
        string name;
        vector<int> polyclasses;
};

, a Polygon class (that I have to sort in Model's polyclasses list) :

class polygone {
    public:
        polygone();
        void affect(int id, int coord){
            ABC[id]=coord;
        }
    private:
        int id;
        float ABC[9]={0.0};
};

I wanted to code a function (cf. "builder") that instanciate n Polygon classes and sort them (with their memory addresses like an id) in an array ("polyclasses" from Model class). So, I don't arrive. Here is a bit of my builder function not acomplished :

void builder(){
    int from = 0; int subfrom = 0;
    for(int i=0; i < numberPoly - 1; i++){
        from = 0; subfrom = 0;
        polygone poly();
        !!! need to put that instance in Model's polygon list !!!
        ...
        for(int j=from; j < (polycoord.size())-1; j++){
            poly.affect(subfrom, polycoord[j]) ...
            subfrom++;
        }
        from += 3;
    }
}

This is for my first c++ project. I'm coding a light 2d engine.

1

1 Answers

2
votes

You need to store pointer of instances in your vector and allocate your objects with new keyword. At destructor of your model yo uwill need to deletethe object to avoid a memory leak.

// Model.h
// Class name should begin with uppercase by convention
class Model{
    public:
        Model();
        ~Model();
        void builder(); 
        // Implementation should go in cpp file
        void affect(int id, int address);
    private:
        // Having a m_ prefix on private variable is useful to make your code more readable so a reader can easily know if a variable is private or not
        string m_name;
        vector<Polygon*> m_polyclasses;
};

// Polygone.h
class Polygone {
public:
    Polygone();
    // Don't forget destructor
    ~Polygone();
    // Implementation should go in cpp file
    void affect(int id, int address);
private:
    int m_id;
    // Use std::array in C++ and give meaningful name to your variable
    // float m_ABC[9]={0.0}; is replaced by :
    std::array<float, 9> m_coordinates;
};

// Model.cpp  
void Model::builder() {
    int from = 0; int subfrom = 0;
    for(int i=0; i < numberPoly - 1; i++){
        from = 0; subfrom = 0;
        Polygone * poly = new Polygone();
        // A pointer of poly is now stored in Model
        this->polyclasses.push_back(poly);
        // Your polygone object should initialized in the constructor or in a member function of the class Polygone.
        for(int j=from; j < (polycoord.size())-1; j++){
            poly->affect(subfrom, polycoord[j]) ...
            subfrom++;
        }
        from += 3;
    }
}

Model::~Model() {
    for(auto p: this->polyclasses) {
        // Avoid memory leak
        delete p; 
    }
    this->polyclasses.clear();
}

You can also store a std::unique_ptr instead of a plain pointer. In that case you don't need to delete.