c风格的转型和c++多继承打架...
时间:2010-08-09 来源:Li_Shugan1
<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"Cambria Math"; panose-1:2 4 5 3 5 4 6 3 2 4; mso-font-charset:1; mso-generic-font-family:roman; mso-font-format:other; mso-font-pitch:variable; mso-font-signature:0 0 0 0 0 0;} @font-face {font-family:Calibri; panose-1:2 15 5 2 2 2 4 3 2 4; mso-font-charset:0; mso-generic-font-family:swiss; mso-font-pitch:variable; mso-font-signature:-1610611985 1073750139 0 0 159 0;} @font-face {font-family:新宋体; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 135135232 16 0 262145 0;} @font-face {font-family:"\@新宋体"; panose-1:2 1 6 9 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:modern; mso-font-pitch:fixed; mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-unhide:no; mso-style-qformat:yes; mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph {mso-style-priority:34; mso-style-unhide:no; mso-style-qformat:yes; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; text-indent:21.0pt; mso-char-indent-count:2.0; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:11.0pt; font-family:"Calibri","sans-serif"; mso-ascii-font-family:Calibri; mso-ascii-theme-font:minor-latin; mso-fareast-font-family:宋体; mso-fareast-theme-font:minor-fareast; mso-hansi-font-family:Calibri; mso-hansi-theme-font:minor-latin; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi; mso-font-kerning:1.0pt;} .MsoChpDefault {mso-style-type:export-only; mso-default-props:yes; mso-bidi-font-family:"Times New Roman"; mso-bidi-theme-font:minor-bidi;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;} @page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;} div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1236168037; mso-list-type:hybrid; mso-list-template-ids:1987069974 2127984910 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l0:level1 {mso-level-tab-stop:none; mso-level-number-position:left; margin-left:33.0pt; text-indent:-18.0pt;} ol {margin-bottom:0cm;} ul {margin-bottom:0cm;} -->
各种 Crash 问题始终幽灵一般地围绕着 C++ 程序员,,如果只是个指针没判空,那观察家也没什么,可有时程序真是挂得莫名其妙,这不,今天我就被这“死亡之吻”狠狠地吻了一口。程序结构模拟如下图, Sign 继承自 Object 和 Feature,Context 中有一 Feature 的数组,其中装的有指向 Sign 的指针 .
Sign 的申明 像这个样子:
class Sign :public Feature,public Object
Output 是这样耿实现的:
void Context::Output()
{
for (int iPos = 0;iPos < m_vFeature.size();++iPos)
{
Object* pObject = (Object*)m_vFeature[iPos] ;
cout<<pObject->ToString()<<endl;
}
}
程序一运行就挂了,当然,实际工程中的代码不像上面这样直接,那个Sign 也是历经了几世几劫才到了Output 中的, m_vFeature 也不是用STL 的vector 实现的,而是一个只接收void* 的数组。所以错误是很承隐蔽的。
Debug, 我头脑发晕,眼前直冒金花,好不容易才让我逮到那个元凶 -- 该死的 C 风格转型 :
Object* pObject = (Object*) m_vFeature[iPos]
问题找到了,改成下面的形式:
Object* pObject = dynamic_cast<Object*>((Feature*) m_vFeature[iPos]);
编译,运行,Crash 如愿消失。其实把Sign 的申明改为: class Sign: public Object ,public Feature.
也可解决问题。
看到这里,大家应该都明白到底发生了,没明白的可以看图:
Feature |
Object |
Other’s member |
Sign 在在存入 Context 时转化为 void* ,而要 output 中直接将其拿出来,用 C 风格转型转化为 Object 指针,就相当于将 Feature 解释成了 Object ,而如果用 dynamic_cast 编译器会自动帮我们加上一个 offset ,就没有这个问题啦。
经过这一劫,惨烈地现实又给我好好地上了一课:
1. 一个好的设计真的很重要,其实如果设计得当,是不会出现上面的问题的。
2. 在使用 C++ 时就忘了 C 吧。
模拟程序见附件 :