unique函数_C++核心准则C.35:基类的析构函数必须满足的条件
C.35: A base class destructor should be either public and virtual, or protected and nonvirtual
基類(lèi)的析構(gòu)函數(shù)要么是公開(kāi)的虛函數(shù),要么是保護(hù)的非虛函數(shù)
Reason(原因)
To prevent undefined behavior. If the destructor is public, then calling code can attempt to destroy a derived class object through a base class pointer, and the result is undefined if the base class's destructor is non-virtual. If the destructor is protected, then calling code cannot destroy through a base class pointer and the destructor does not need to be virtual; it does need to be protected, not private, so that derived destructors can invoke it. In general, the writer of a base class does not know the appropriate action to be done upon destruction.
為了避免無(wú)定義的行為。如果析構(gòu)函數(shù)是公有的,那么調(diào)用側(cè)的代碼就會(huì)嘗試使用基類(lèi)指針?shù)N毀派生類(lèi)的對(duì)象,在基類(lèi)的析構(gòu)函數(shù)為非虛函數(shù)時(shí)其結(jié)果時(shí)沒(méi)有定義的。如果析構(gòu)函數(shù)時(shí)保護(hù)的,那么調(diào)用側(cè)代碼就無(wú)法通過(guò)基類(lèi)類(lèi)型指針?shù)N毀派生類(lèi)對(duì)象,這是析構(gòu)函數(shù)就沒(méi)有必要一定是虛函數(shù)。析構(gòu)函數(shù)是保護(hù)而不是私有的,這樣派生類(lèi)的析構(gòu)函數(shù)才能調(diào)用它。通常,基類(lèi)的設(shè)計(jì)者不會(huì)知道在析構(gòu)函數(shù)中應(yīng)該執(zhí)行什么樣的動(dòng)作。
Discussion(討論)
See this in the Discussion section:參見(jiàn)討論章節(jié):
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#Sd-dtor.
Example, bad(反面示例)
struct Base { // BAD: implicitly has a public nonvirtual destructor
virtual void f();
};
struct D : Base {
string s {"a resource needing cleanup"};
~D() { /* ... do some cleanup ... */ }
// ...
};
void use()
{
unique_ptr p = make_unique();
// ...
} // p's destruction calls ~Base(), not ~D(), which leaks D::s and possibly more
Note(注意)
A virtual function defines an interface to derived classes that can be used without looking at the derived classes. If the interface allows destroying, it should be safe to do so.虛函數(shù)定義了派生類(lèi)的接口,它可以在不關(guān)注派生類(lèi)的情況下使用。如果接口允許對(duì)象,那么這個(gè)銷(xiāo)毀過(guò)程應(yīng)該是安全的。
Note(注意)
A destructor must be nonprivate or it will prevent using the type:析構(gòu)函數(shù)必須是非私有的,除了它不想被別人用。(這樣可以由類(lèi)自己控制銷(xiāo)毀,譯者注)
class X {
~X(); // private destructor
// ...
};
void use()
{
X a; // error: cannot destroy
auto p = make_unique(); // error: cannot destroy
}
Exception(例外)
We can imagine one case where you could want a protected virtual destructor: When an object of a derived type (and only of such a type) should be allowed to destroy another object (not itself) through a pointer to base. We haven't seen such a case in practice, though.我們可以想象一種需要保護(hù)的虛函數(shù)析構(gòu)函數(shù)的情況:當(dāng)希望允許派生類(lèi)的對(duì)象(只有這個(gè)類(lèi)型)通過(guò)基類(lèi)指針?shù)N毀另外一個(gè)對(duì)象(不是它自己)時(shí)。但是我們還沒(méi)有在實(shí)際的開(kāi)發(fā)中遇到這種情況。
Enforcement(實(shí)施建議)
- A class with any virtual functions should have a destructor that is either public and virtual or else protected and nonvirtual.
- 擁有虛函數(shù)的類(lèi)的虛函數(shù)要么是公開(kāi)的虛函數(shù),要么是保護(hù)的非虛函數(shù)。
譯者注:擁有虛函數(shù)一般就意味著它有派生類(lèi)。
原文鏈接:
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#c35-a-base-class-destructor-should-be-either-public-and-virtual-or-protected-and-nonvirtual
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
更多文章請(qǐng)關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌?#xff01;
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎?#xff01;
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
以上是生活随笔為你收集整理的unique函数_C++核心准则C.35:基类的析构函数必须满足的条件的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 重型直升机掉出来的重机枪手等级
- 下一篇: c++ dicom图像切割_【高训智造】