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?

  • Well actually the link and the pointer are different things. Context - places - Patient * as you do places = &p it is ugly, you will then do delete[] places i.e. in essence, the call to delete an array for 1 item. UB, the memory may well suffer. - pavel
  • What are your places , where and how is it declared, initialized, etc.? And - you pass something to Patient as &p , then as new - 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 ... - Harry
  • places - Patient type pointer, initialized nullptr default - saninstein

0