ES.60: Avoid new and delete outside resource management functionsES.60:避免在资源管理函数之外使用new和delete
Reason(原因)
Direct resource management in application code is error-prone and tedious.
在应用代码中直接进行资源管理麻烦且易错。
Note(注意)
This is also known as the rule of "No naked new!"
本准则也被称为“禁止暴露的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操作永远不发生。
See also: R: Resource management
参见:R:资源管理规则群组
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r-resource-management
Enforcement(实施建议)
Flag naked news and naked deletes.
标记暴露的new和delete操作。
原文链接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#es60-avoid-new-and-delete-outside-resource-management-functions
觉得本文有帮助?请分享给更多人。
关注微信公众号【面向对象思考】轻松学习每一天!
面向对象开发,面向对象思考!
,