前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++核心准则R.4: 原始引用(T&)不包含所有权

C++核心准则R.4: 原始引用(T&)不包含所有权

作者头像
面向对象思考
发布2020-03-26 10:00:01
4710
发布2020-03-26 10:00:01
举报

R.4: A raw reference (a T&) is non-owning

R.4: 原始引用(T&)不包含所有权

Reason(原因)

There is nothing (in the C++ standard or in most code) to say otherwise and most raw references are non-owning. We want owners identified so that we can reliably and efficiently delete the objects pointed to by owning pointers.

这一点不存在例外(无论是C++标准还是大部分代码中),实际上大多数原始引用就是不包含所有权的。我希望所有者被明确下来以便我们可以可靠而且高效的删除所有权指针指向的对象。

Example(示例)

代码语言:javascript
复制
void f()
{
    int& r = *new int{7};  // bad: raw owning reference
    // ...
    delete &r;             // bad: violated the rule against deleting raw pointers
}

See also: The raw pointer rule:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Rr-ptr

参见原始指针原则

Enforcement(实施建议)

See the raw pointer rule:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGidelines.md#Rr-ptr

参见原始指针原则

原文链接:

https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#r3-a-raw-pointer-a-t-is-non-owning

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • R.4: A raw reference (a T&) is non-owning
  • R.4: 原始引用(T&)不包含所有权
  • Reason(原因)
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档