0

for example I have a code which I need to insert every time a worker for the first worker its easy because all I do is

Worker** list_of_workers=new Worker*[1];
list_of_workers[0]=new Worker(name ,id , seniority );

for the next worker I want to increase the size of list of workers and add another worker and i really stuck on this one.

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 10
  • what type is your "list of workers"? – jasonlg3d Aug 17 '21 at 20:32
  • It is not possible to resize raw arrays. – Quimby Aug 17 '21 at 20:33
  • 1
    You must reallocate and copy/move elements to the new array – AndyG Aug 17 '21 at 20:36
  • 2
    If barbaric practices (or strange circumstances) have disallowed the use of `vector` , all you can do is allocate a new, larger array, copy the data from the original array to the larger array, delete the original array, and replace the original array with the larger array. – user4581301 Aug 17 '21 at 20:37
  • std::list> workers; workers.push_back(std::make_unique()); – jasonlg3d Aug 17 '21 at 20:37
  • 1
    If you are not allowed to use `std::vector`, then you need to create a new dynamic array of the required size, copy the old array data to the new array, delete the old array, and assigned the new array pointer to your variable. `std::vector` implementations handle this for you. EDIT: Dangit, @user45, you beat me to it step-for-step – JohnFilleau Aug 17 '21 at 20:37
  • Your answer is in this link. https://stackoverflow.com/questions/12032222/how-to-dynamically-increase-the-array-size – Sayed Mohammad Javad Fatemi Aug 17 '21 at 20:38
  • Note: `std::list` is specified in such a way that it is almost always implemented as a doubly linked list. This makes it a poor drop in replacement for an array. It's also unusual to use `std::list`s of pointers, smart or otherwise, because `std::list` has very forgiving [iterator invalidation](https://stackoverflow.com/questions/6438086/iterator-invalidation-rules) – user4581301 Aug 17 '21 at 20:42
  • 1
    @JohnFilleau It is rare for me to out type anyone, so I must have started earlier. – user4581301 Aug 17 '21 at 20:42
  • I did new allocation and than i did a for loop and i tried to do this for(int i=0;i – Hen Simkin Aug 17 '21 at 21:01
  • `std::vector list_of_workers; list_of_workers.push_back(Worker{name, id, seniority}); list_of_workers.push_back(Worker{name2, id2, seniority2});` In modern C++, unless you are making your own containers or smart pointers, you probably don't need to use `new` anymore. – Eljay Aug 17 '21 at 21:01
  • @HenSimkin carefully consider when you delete the workers. Since when you expand the list you still want the workers, it does not make sense to point the larger list at the workers and then delete the workers. Larger list is left pointing to dead objects. You only want to delete a worker when you are done using the worker. But consider: Do you really want pointers to dynamically allocated and independently managed workers? That adds an extra level of management that will lead you to conceptual problems and may give little in return. – user4581301 Aug 17 '21 at 22:28

0 Answers0