If I have an array created like this:
MyType *array = new MyType[10];
And I want to overwrite one of the elements, do I have to delete first the old element like this:
delete &array[5];
array[5] = *(new MyType());
Or is this completely wrong and do I have to work with something like “pointers to pointers” to fix this job? If so, how please….
Thanks
It’s an array of values, not of pointers. So you’d just do
This requires
MyTypeto support the assignment operator.Incidentally, there’s rarely a need for manual array allocation like this in C++. Do away with the
newanddeleteand usestd::vectorinstead:Note, there’s no need to delete anything.