Thanks for the quick and clear answer to my previous question. The next question is this: the proposed solution to the problem (if I understood everything correctly) implies that the user must know the structure of all the pieces of hardware that he needs to describe and, when initializing the assembly, must immediately bring in all the components. This is often unrealistic. That is, it is necessary to allow the user to add new elements to the array property after initializing the assembly class. I wrote something similar here, but it doesn’t inspire confidence in me (although compiling it is already good), comment, please, my works

class clPart { //деталь public: int prtHeight; int prtWidth; int prtLength; }; class clUnit { //сборка private: clPart* pPart; int prtCount; public: clUnit() { prtCount=1; pPart = new clPart[prtCount]; } void addPart(clPart*); }; void clUnit::addPart(clPart* Part){ prtCount++; pPart = new clPart[prtCount]; pPart=Part; //вот это мне очень не нравится, мне кажется это ошибка }; 

Thanks in advance for the constructive comments without the elements of trolling and sending to Straustrup and Schild)

    2 answers 2

    This code has a lot of problems :)

    For example, when you do pPart = new clPart [ptrCount], you have

    1. create a continuous array of ptrCount objects of type clPart ;
    2. pPart changes its value to the address of the new array, while the address of the old array is lost, so there is a memory leak; not to mention the lost values.

    That is, instead of adding a part, you re-create an array of ptrCount details, losing the old array.

    This task is better for you to solve with the help of STL, namely, the standard vector std::vector . This is just what you need - a variable length array. That is, inside clUnit you will have a variable.

     std::vector<clPart> parts; 

    Add a new item like this: parts.push_back(Part) .

    Refer to the elements of the vector as with C-like arrays: parts[i] .

    And you "free then" get the size of the array: parts.size() .

    More details about the vectors can be found here: http://www.cppreference.com/wiki/ru/container/vector/start

    • correction: you should write std :: vector <clPart *> parts; - IAZ
    • In a good way, yes. But I deliberately left objects, not pointers, to avoid problems with unallocated memory. - y0prst
     clUnit::clUnit() { prtCount=0; pPart = 0; } void clUnit::addPart(clPart* Part){ // сначала копируем все элементы из "старого" массива в "новый" clPart* pNewPart = new clPart[++prtCount]; for (int i=0; i<prtCount-1; ++i) pNewPart[i] = pPart[i]; pNewPart[prtCount-1] = Part; // сохраняем в массиве новый элемент delete[] pPart; pPart = pNewPart; // подменяем старый массив на новый } }; 

    In general, the code should be clear what is happening there, add that all the same, you should use std :: vector to store pointers to clPart. Just for the sake of greater productivity and efficiency. The code given is not optimal for cases of frequent addition of new elements.

    • It could be relatively easy to make it optimal for this case, if you do not create a new array and do not copy pointers with each addition. - cy6erGn0m
    • @ cy6erGn0m how do you propose to add a new element to an array with a fixed size? - IAZ