mardi 29 septembre 2020

Unable to delete an object from heap allocated array

I am a beginner, and I want to delete an item from an array, non-STL.

This doesn't seem possible? Below. So, out of share curiosity, how can I delete or remove an item from a regular array, I am not using STL. I want to know how this can be done with one dynamic array that's been allocated on the HEAP.

Object here can be a random class e.g. a Monster class.

#include <iostream>

int main()
{
    int size = 10;
    Object* o = new Object[size];
    
    for(int i = 0; i<size; i++){
        o[i] = new Object("Monster " + i);
    }
    
    for(Object* ptr = o; ptr!=o+size; o++){
        delete ptr;
    }
}

Aucun commentaire:

Enregistrer un commentaire