lundi 8 juillet 2019

How to delete dynamic allocated class object in list

I just create list of Coordi object as below inside of a functions. Coordi is class object which I defined.

list<Coordi> LstOfCoordi;
for (int i = 0; i < 10; ++i)
{
    Coordi *tmp = new Coordi();
    LstOfCoordi.push_back(tmp);
} 

I want release the list and each objects to the system before program termination.

If each element of list should have to be released, how to handle it ? I tried to below codes to use delete each of them, but it made compile error.

for (int i = 0; i < 10; ++i)
{
    myClass tmp = LstOfMyCalss.back();
    delete &tmp;
    LstOfMyClass.pop_back();
} 

So, I am wondering deletion from the list is enough or not. At a glance with below codes, each element was allocated and they are not released properly when the list destroyed.

for (int i = 0; i < 10; ++i)
{
    //myClass tmp = LstOfMyCalss.back();
    //delete &tmp;
    LstOfMyClass.pop_back();
} 

Please let me know the best way to release all allocated objects.

Aucun commentaire:

Enregistrer un commentaire