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:///isocpp/CppCoreGuidelines/blob/master/#Rr-ptr​

参见​​原始指针原则​​。

 

Enforcement(实施建议)

See the raw pointer rule:

​https:///isocpp/CppCoreGuidelines/blob/master/#Rr-ptr​

参见​​原始指针原则​

 

原文链接:

​https:///isocpp/CppCoreGuidelines/blob/master/#r3-a-raw-pointer-a-t-is-non-owning​

 


 

觉得本文有帮助?欢迎点赞并分享给更多的人。

阅读更多更新文章,请关注微信公众号【面向对象思考】