📄 book_text12.htm
字号:
,系统将重新计算该表达式,并激活调试器。同TRACE宏一样,ASSERT宏只DEBUG版本中起作用,在RELEASE版本中ASSERT宏将被忽略。<O:P>
</O:P></SPAN></SPAN></P>
<P class=MsoNormal
style="TEXT-INDENT: 24pt; mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"> <O:P>
</O:P></SPAN></P>
<H3 style="mso-line-height-alt: 1.3pt"><A name=_Toc419881658><SPAN
lang=EN-US style="mso-bookmark: _Toc419740476">2.3
ASSERT_VALID</SPAN></A><SPAN
style="mso-bookmark: _Toc419740476"><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">宏的利用以及类的</SPAN><SPAN
lang=EN-US>AssertValid()</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'; mso-ascii-font-family: 'Times New Roman'">成员函的重载</SPAN></SPAN></H3>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>ASSERT_VALID宏用来在运行时检查一个对象的内部合法性,比如说现在
有一个学生对象,我们知道每个学生的年龄一定大于零,若年龄小于零,则该学生对象肯定有问题。事实上,ASSERT_VALID宏就是转化为对象的成员函数AssertValid()的调用,只是这种方法更安全。它的参数是一个对象指针,通过这个指针来调用它的AssertValid()成员函数。<O:P>
</O:P></SPAN></P>
<P class=MsoNormal
style="TEXT-INDENT: 20pt; mso-line-height-alt: 1.3pt"><SPAN
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'">与此相配套,每当我们创建从<SPAN
lang=EN-US>Cobject类继承而来的一个新的类时,我们可以重载该成员函数,以执行特定的合法性检查。<O:P>
</O:P></SPAN></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"> <O:P>
</O:P></SPAN></P>
<H3 style="mso-line-height-alt: 1.3pt"><A name=_Toc419881659><SPAN
lang=EN-US
style="FONT-FAMILY: 宋体; mso-hansi-font-family: Times New Roman; mso-bookmark: _Toc419740477">2.4对象的DUMP函数的利用</SPAN></A><SPAN
lang=EN-US
style="FONT-FAMILY: 宋体; mso-hansi-font-family: 'Times New Roman'"><O:P>
</O:P></SPAN></H3>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>Dump
函数用来按指定的格式输出一个对象的成员变量,来帮助你诊断一个对象的内部情况。与AssertValid成员函数一样,Dump也是Cobject
类的成员函数。Dump函数的参数是一个CdumpContext对象,你可以象利用流一样往向这个对象中输入数据。当你创建一个Cobject继承而来的
新类时,你可以按如下步骤重载你自己的Dump函数:<O:P> </O:P></SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: -21.25pt; mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>(1) 调用基类的Dump函数,以输出基类的内容;<O:P> </O:P></SPAN></P>
<P class=MsoNormal
style="MARGIN-LEFT: -21.25pt; mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"><SPAN
style="mso-spacerun: yes">
</SPAN>(2) 向Cdumpcontest对象输出该类的数据.<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'">例如<SPAN
lang=EN-US>,典型的Dump函数定义如下:<O:P> </O:P></SPAN></SPAN></P>
<P class=MsoNormal
style="mso-line-height-alt: 1.3pt; mso-outline-level: 1"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black">#ifdef
_DEBUG<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black">void
CPerson::Dump( CDumpContext& dc ) const<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black">{<O:P>
</O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black"><SPAN
style="mso-spacerun: yes"> </SPAN>// call base
class function first<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black"><SPAN
style="mso-spacerun: yes"> </SPAN>CObject::Dump(
dc );<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"> <O:P>
</O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'"><SPAN
style="mso-spacerun: yes"> </SPAN>// now do the
stuff for our specific class<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black"><SPAN
style="mso-spacerun: yes"> </SPAN>dc <<
"last name: " << m_lastName << "\n"<O:P>
</O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black"><SPAN
style="mso-spacerun: yes">
</SPAN><< "first name: " << m_firstName <<
"\n";<O:P> </O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black">}<O:P>
</O:P></SPAN></P>
<P class=MsoNormal style="mso-line-height-alt: 1.3pt"><SPAN
lang=EN-US
style="FONT-SIZE: 10.5pt; BACKGROUND: #d9d9d9; FONT-FAMILY: 'Courier New'; mso-bidi-font-size: 10.0pt; mso-bidi-font-family: 'Times New Roman'; mso-shading: white; mso-pattern: gray-15 black">#endif<O:P>
</O:P></SPAN></P><SPAN lang=EN-US
style="FONT-SIZE: 10.5pt; FONT-FAMILY: 宋体; mso-bidi-font-size: 10.0pt; mso-hansi-font-family: 'Times New Roman'; mso-bidi-font-family: 'Times New Roman'; mso-ansi-language: EN-US; mso-fareast-language: ZH-CN; mso-bidi-language: AR-SA"><SPAN
style="mso-spacerun: yes">
</SPAN>你可能已经注意到整个函数的定义都包含在#ifdef _DEBUG
和#endif中,这使得Dump成员函数只在DEBUG版本中发生作用,而对RELEASE版本不发生作用。</SPAN>
<P> </P></TD>
<TD width="3%" height=15></TD></TR>
<TR>
<TD width="2%" height=1></TD>
<TD vAlign=top width="95%" height=1><PRE></PRE></TD>
<TD width="3%"
height=1></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -