【发布时间】:2020-04-06 15:29:28
【问题描述】:
我只是想知道在 C++ 中是否需要手动销毁抛出的对象?
在下面的代码中,如何销毁抛出的0?
try
{
...
throw 0;
}
catch(int i)
{
// How to destroy the thrown 0?
}
以下代码中,如何销毁抛出的CString对象?
try
{
...
throw CString(_T("Hello"));
}
catch(CString& str)
{
// How to destroy the thrown str?
}
在下面的代码中,我可以销毁抛出的对象,因为它是作为 CString* 从堆中分配的
try
{
...
throw new CString(_T("Hello"));
}
catch(CString* lpStr)
{
delete lpStr;
}
【问题讨论】:
-
在前两个示例中,您不要“破坏”对象。这将作为异常处理的一部分自动发生。
-
更多信息here
标签: c++ exception try-catch throw