If I manage to construct objects in C++ by doing
Object o;
instead of
Object *o = new Object();
in every case, do I ever need to call delete
or will all memory be managed automatically?
No. That object is allocated on the stack, and will be destroyed automatically when it goes out of scope. That includes freeing its memory and calling the destructor.
You cannot call delete
on an object that was not allocated with new
. Objects will be destroyed when they go out of scope.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.