C++核心準則R.4: 原始引用(T&)不包含所有權

C++核心準則R.4: 原始引用(T&)不包含所有權

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

<code>void f()
{
int& r = *new int{7}; // bad: raw owning reference
// ...
delete &r; // bad: violated the rule against deleting raw pointers
}/<code>

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


覺得本文有幫助?請分享給更多人。

關注【面向對象思考】輕鬆學習每一天!

面向對象開發,面向對象思考!


分享到:


相關文章: