前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作

C++核心准则讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作

作者头像
面向对象思考
发布2020-12-31 14:57:40
5280
发布2020-12-31 14:57:40
举报

Discussion: If a class is a resource handle, it needs a constructor, a destructor, and copy and/or move operations

讨论:如果一个类是资源句柄,则它需要一个构造函数,一个析构函数以及复制和/或移动操作

Reason(原因)

To provide complete control of the lifetime of the resource. To provide a coherent set of operations on the resource.

提供对资源生命周期的完全控制。在资源上提供连贯的操作集。

Example(示例)

代码语言:javascript
复制
??? Messing with pointers
Note(注意)

If all members are resource handles, rely on the default special operations where possible.

如果所有成员都是资源句柄,请尽可能依靠默认的特殊操作。

代码语言:javascript
复制
template<typename T> struct Named {
    string name;
    T value;
};

Now Named has a default constructor, a destructor, and efficient copy and move operations, provided T has.

现在,Named类具有默认的构造函数,析构函数以及有效的复制和移动操作(如果T具有)。

Enforcement(实施建议)

In general, a tool cannot know if a class is a resource handle. However, if a class has some of the default operations, it should have all, and if a class has a member that is a resource handle, it should be considered as resource handle.

通常,工具无法知道类是否是资源句柄。但是,如果类具有某些默认操作,则应具有全部默认操作,并且如果类具有作为资源句柄的成员,则应将其视为资源句柄。

原文链接https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#discussion-if-a-class-is-a-container-give-it-an-initializer-list-constructor

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-12-19,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 面向对象思考 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Discussion: If a class is a resource handle, it needs a constructor, a destructor, and copy and/or move operations
  • Reason(原因)
    • Note(注意)
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档