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

共 1007字,需浏览 3分钟

 ·

2020-03-24 23:23

c51c93b008c9261339d0ee9076e88bfa.webp

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(示例)

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




觉得本文有帮助?请分享给更多人。

关注【面向对象思考】轻松学习每一天!

面向对象开发,面向对象思考!

浏览 40
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

分享
举报