C++核心準(zhǔn)則ES.60:避免在資源管理函數(shù)之外使用new和delete

ES.60: Avoid?new?and?delete?outside resource management functions
ES.60:避免在資源管理函數(shù)之外使用new和delete
Reason(原因)
Direct resource management in application code is error-prone and tedious.
在應(yīng)用代碼中直接進(jìn)行資源管理麻煩且易錯(cuò)。
Note(注意)
This is also known as the rule of "No naked?new!"
本準(zhǔn)則也被稱為“禁止暴露的new操作”
Example, bad(反面示例)
void f(int n)
{
auto p = new X[n]; // n default constructed Xs
// ...
delete[] p;
}
There can be code in the?...?part that causes the?delete?never to happen.
在...省略的位置的代碼可以讓delete操作永遠(yuǎn)不發(fā)生。
See also:?R: Resource management
參見:R:資源管理規(guī)則群組
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management
Enforcement(實(shí)施建議)
Flag naked?news and naked?deletes.
標(biāo)記暴露的new和delete操作。
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es60-avoid-new-and-delete-outside-resource-management-functions
覺得本文有幫助?請分享給更多人。
關(guān)注微信公眾號【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_發(fā),面向?qū)ο笏伎迹?/span>
