Do i need to delete newP (temp) after using it?

74 Views Asked by At

I asked a question about a code and someone recommended i used newP to add a class to a vector, it works, but i dont fully understand the concept behind that way of doing.

string creaPersoFini("y");
    vector<Personnage> tableauPerso;    // Tableau de Personnage, pas de string
    string nomPerso;
    int nbPerso(3);

    cout << "Trois personnages maximum, pour l'instant." << endl;
    while(creaPersoFini!="n" && nbPerso < 4)
    {
        cout << "Entrez le nom du personnage : ";
        cin >> nomPerso;
        Personnage newP(nomPerso);              // new temp person
        tableauPerso.push_back(newP);           // add the person
        cout << "Voulez vous creer un autre personnage ? y/n ";
        cin >> creaPersoFini;
    }

Is this correct ? Do i need to delete the newP after adding it to the vector ? I know that if you create a pointer with new it is necessary to delete it afterward, is this what is happening here or not at all ?

I tried to look on internet and stackoverflow for an answer, but i only found parts of answers or quesitons/answers that are too complex for my current knowledge of C++.

If someone could explain to me in a simple way the difference between new/delete, newP and temporary using like these (or point me in a direction where i can find an answer) i'd be grateful !

Here is the link to the question where he recommended it, i used it because it works but since there was disagrement in the comments i'm not sure, and i'd like to be.

0

There are 0 best solutions below