When to use “delete”?

前端 未结 5 1708
天命终不由人
天命终不由人 2020-12-06 07:36

I want to store 10 Obj object in objList, but i don\'t know when is appropriate use delete in this case. If i use delete Obj;

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-06 08:39

    Yes, you should.

    There should be a corresponding delete for every new in your program.

    But your program doesn't need dynamic allocation:

    Obj obj;
    obj.u = i;
    obj.v = i + 1;
    objList.push_back(obj);
    

    Also, your syntax was wrong - objList.push_back(*Obj); // should be *obj, not *Obj

提交回复
热议问题