Deleting Object Arrays ?

Is it possible to delete a certain object from an array ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream>
using namespace std;

class animal
{
  //random stuff here
}

//following stuff

int main()
{
   animal animalGroup = new animal[5]
   
   //say you already initilized all the animals already from the array

  delete animalGroup[3]; // say if I wanted to delete the third one only

  return 0;
}


Is it possible if I wanted to do so ?
No. The size of an array is constant for its entire lifetime.

You could
1) use vectors
2) replace animalGroup[3] with some special dummy animal to indicate that this array element is empty
3) copy animalGroup[4] over to animalGroup[3] and pretend that animalGroup[4] no longer exists (e.g. by maintaining a size variable)
4) create a new array of size 4, copy all elements except animalGroup[3] over, and delete the old array
Topic archived. No new replies allowed.