更新时间:2023-11-11 12:09:10
推荐的方式是按值抛出,按引用捕获.
您的示例代码抛出了一个指针,这是一个坏主意,因为您必须在 catch 站点管理内存.
Your example code throws a pointer, which is a bad idea since you would have to manage memory at the catch site.
如果您真的觉得应该抛出一个指针,请使用智能指针,例如 shared_ptr
.
If you really feel you should throw a pointer, use a smart pointer such as shared_ptr
.
无论如何,Herb Sutter 和 Alexei Alexandrescu 在他们的 C++ 编码标准一书中很好地解释了这一点,我对此进行了解释.