Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
如果我设法通过这样做在 C++ 中构造对象
Object o;
代替
Object *o = new Object();
在每种情况下,我是否需要调用delete或是否会自动管理所有内存?
delete
不,该对象是在堆栈上分配的,并且会在超出范围时自动销毁。这包括释放它的内存和调用析构函数。
您不能调用delete未分配的对象new。对象超出范围时将被销毁。
new