I am trying to add an object to an array, it seems to be used, similar, but in that case it was not necessary to add an already existing object to the array. The program completes its work, on the third addition, if not the second. And another question: I can change the object with, as I pass it? This is almost the same as through the pointer?
Please indicate the problem.
Here is the actual code:
void addPacient(Patient &p){ if(p.getAge() < 18){ throw AgeException(); } else{ addDays(); ++count_places; if(places != nullptr){ Patient *temp = places; places = new Patient[count_places]; for(int i = 0; i < count_places - 1; i++){ places[i] = temp[i]; } delete []temp; places[count_places - 1] = p; //cout << places[count_places - 1].getName() << endl; } else{ places = &p; //cout << places->getName() << endl; } } } And another question: So, how do I pass an object to a method, can I change it? Is this the same as passing through a pointer?
places-Patient *as you doplaces = &pit is ugly, you will then dodelete[] placesi.e. in essence, the call to delete an array for 1 item. UB, the memory may well suffer. - pavelplaces, where and how is it declared, initialized, etc.? And - you pass something toPatientas&p, then asnew- you either have a memory leak or an attempt to remove what you cannot delete ... so that it lies is not the worst result :), where worse if she worked ... - Harryplaces-Patienttype pointer, initializednullptrdefault - saninstein