C++核心準(zhǔn)則T.?81:不要混用繼承層級(jí)和數(shù)組

T.81: Do not mix hierarchies and arrays
T.81:不要混用繼承層級(jí)和數(shù)組
Reason(原因)
An array of derived classes can implicitly "decay" to a pointer to a base class with potential disastrous results.
派生類的數(shù)組可以隱式退化為可能帶來(lái)災(zāi)難性后果的指向基類的指針。
Example(示例)
Assume that?Apple?and?Pear?are two kinds of?Fruits.
假定Apple和Pear是兩種Fruit。
void maul(Fruit* p)
{
*p = Pear{}; // put a Pear into *p
p[1] = Pear{}; // put a Pear into p[1]
}
Apple aa [] = { an_apple, another_apple }; // aa contains Apples (obviously!)
maul(aa);
Apple& a0 = &aa[0]; // a Pear?
Apple& a1 = &aa[1]; // a Pear?
Probably,?aa[0]?will be a?Pear?(without the use of a cast!). If?sizeof(Apple) != sizeof(Pear)?the access to?aa[1]?will not be aligned to the proper start of an object in the array. We have a type violation and possibly (probably) a memory corruption. Never write such code.
很有可能aa[0]是一個(gè)Pear(不需要類型轉(zhuǎn)化)。如果sizeof(Apple)!=sizeof(Pear),對(duì)于aa[1]的訪問(wèn)位置就不會(huì)正確開(kāi)始于下個(gè)對(duì)象。我們會(huì)遇到類型違反和可能的(幾乎一定)內(nèi)存破壞。永遠(yuǎn)不要這樣寫(xiě)代碼。
Note that?maul()?violates the a?T*?points to an individual object rule.
Alternative: Use a proper (templatized) container:
注意maul()已經(jīng)違反了使用T*或onwer
void maul2(Fruit* p)
{
*p = Pear{}; // put a Pear into *p
}
vector va = { an_apple, another_apple }; // va contains Apples (obviously!)
maul2(va); // error: cannot convert a vector to a Fruit*
maul2(&va[0]); // you asked for it
Apple& a0 = &va[0]; // a Pear?
Note that the assignment in?maul2()?violated the?no-slicing rule.
注意maul2()中的賦值操作違反了不要分割對(duì)象原則。
Enforcement(實(shí)施建議)
Detect this horror!
檢出這種可怕的問(wèn)題!
原文鏈接
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#t81-do-not-mix-hierarchies-and-arrays
新書(shū)介紹
《實(shí)戰(zhàn)Python設(shè)計(jì)模式》是作者最近出版的新書(shū),拜托多多關(guān)注!

本書(shū)利用Python 的標(biāo)準(zhǔn)GUI 工具包tkinter,通過(guò)可執(zhí)行的示例對(duì)23 個(gè)設(shè)計(jì)模式逐個(gè)進(jìn)行說(shuō)明。這樣一方面可以使讀者了解真實(shí)的軟件開(kāi)發(fā)工作中每個(gè)設(shè)計(jì)模式的運(yùn)用場(chǎng)景和想要解決的問(wèn)題;另一方面通過(guò)對(duì)這些問(wèn)題的解決過(guò)程進(jìn)行說(shuō)明,讓讀者明白在編寫(xiě)代碼時(shí)如何判斷使用設(shè)計(jì)模式的利弊,并合理運(yùn)用設(shè)計(jì)模式。
對(duì)設(shè)計(jì)模式感興趣而且希望隨學(xué)隨用的讀者通過(guò)本書(shū)可以快速跨越從理解到運(yùn)用的門(mén)檻;希望學(xué)習(xí)Python GUI 編程的讀者可以將本書(shū)中的示例作為設(shè)計(jì)和開(kāi)發(fā)的參考;使用Python 語(yǔ)言進(jìn)行圖像分析、數(shù)據(jù)處理工作的讀者可以直接以本書(shū)中的示例為基礎(chǔ),迅速構(gòu)建自己的系統(tǒng)架構(gòu)。
覺(jué)得本文有幫助?請(qǐng)分享給更多人。
關(guān)注微信公眾號(hào)【面向?qū)ο笏伎肌枯p松學(xué)習(xí)每一天!
面向?qū)ο箝_(kāi)發(fā),面向?qū)ο笏伎迹?/span>
