📄 chxc.htm
字号:
<P>The sample application in this section uses the MFC debugging class <font color="#008000">CDumpContext</font> and the global <font color="#008000">axfDump</font> object. The debug window output from this demo and the output <font
color="#008000">CFile</font> code are in Listing C.1. To run this application yourself, create a console application as described in <A HREF="index28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index28.htm" target="text">Chapter 28</A>, "Future Explorations," and create an empty C++ source file
called Dump.cpp. Enter this code, build, and run a debug version of the project.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>The Dump project is on the CD in the RefC folder.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><I>Listing C.1—Dump.Cpp—Demonstrates the MFC Debugging Class </I><I>CDumpContext and CFile</I></P>
<pre><font color="#008000">#include <afx.h></font></pre>
<pre><font color="#008000">// _DEBUG defined for debug build</font></pre>
<pre><font color="#008000">class CPeople : public CObject</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">public:</font></pre>
<pre><font color="#008000"> // constructor</font></pre>
<pre><font color="#008000"> CPeople( const char * name );</font></pre>
<pre><font color="#008000"> // destructor</font></pre>
<pre><font color="#008000"> virtual ~CPeople();</font></pre>
<pre><font color="#008000"> #ifdef _DEBUG</font></pre>
<pre><font color="#008000"> virtual void Dump(CDumpContext& dc) const;</font></pre>
<pre><font color="#008000"> #endif</font></pre>
<pre><font color="#008000"> private:</font></pre>
<pre><font color="#008000"> CString * person;</font></pre>
<pre><font color="#008000"> };</font></pre>
<pre><font color="#008000"> // constructor</font></pre>
<pre><font color="#008000"> CPeople::CPeople( const char * name) : person( new CString(name)) {};</font></pre>
<pre><font color="#008000"> // destructor</font></pre>
<pre><font color="#008000"> CPeople::~CPeople(){ delete person; }</font></pre>
<pre><font color="#008000">#ifdef _DEBUG</font></pre>
<pre><font color="#008000"> void CPeople::Dump( CDumpContext& dc ) const</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> CObject::Dump(dc);</font></pre>
<pre><font color="#008000"> dc << person->GetBuffer( person->GetLength() + 1);</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000">#endif</font></pre>
<pre><font color="#008000">int main()</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> CPeople person1("Kate Gregory");</font></pre>
<pre><font color="#008000"> CPeople person2("Clayton Walnum");</font></pre>
<pre><font color="#008000"> CPeople person3("Paul Kimmel");</font></pre>
<pre><font color="#008000"> // Use existing afxDump with virtual dump member function</font></pre>
<pre><font color="#008000"> person1.Dump( afxDump );</font></pre>
<pre><font color="#008000"> // Instantiate a CFile object</font></pre>
<pre><font color="#008000"> CFile dumpFile("dumpout.txt", CFile::modeCreate | </font></pre>
<pre><font color="#008000"> CFile::modeWrite);</font></pre>
<pre><font color="#008000"> if( !dumpFile )</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> afxDump << "File open failed.";</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> else</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> // Dump with other CDumpContext</font></pre>
<pre><font color="#008000"> CDumpContext context(&dumpFile);</font></pre>
<pre><font color="#008000"> person2.Dump(context);</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> return 0;</font></pre>
<pre><font color="#008000"> }</font></pre>
<P>This single file contains a class definition, all the code for the member functions of the class, and a <font color="#008000">main()</font> function to run as a console application. Each of these parts of the file is explained in the next few
paragraphs. The class is a simple wrapper around a CString pointer, which allocates the CString with <font color="#008000">new</font> in the constructor and deletes it in the destructor. It's so simple it's actually useless for anything other then
demonstrating the <font color="#008000">Dump()</font> function.</P>
<P>First, the <font color="#008000"><afx.h></font> header file is included, which contains the <font color="#008000">CObject</font> class definition and provides access to <font color="#008000">afxDump</font>.</P>
<P>Next, this code defines a class <font color="#008000">CPeople</font> derived from <font color="#008000">CObject</font>. Notice the placement of the override of the virtual <font color="#008000">Dump()</font> method and the conditional compiler wrap.
(Any calls to<font color="#008000"> Dump()</font> should be wrapped in just the same way.)</P>
<P>After the constructor and destructor comes the code for <font color="#008000">CPeople::Dump()</font>. Notice how it, too, is wrapped in conditional compiler directives. The call to <font color="#008000">CObject::Dump()</font> takes advantage of the
work done by the MFC programmers, dumping information all objects keep.</P>
<P>Finally, the <font color="#008000">main()</font> function excercises this little class. It creates three instances of the CPeople class, and dumps the first one.</P>
<P>For the second CPeople object, this code creates and opens a CFile object by passing a text string to the constructor. If the open succeeds, it creates a CDumpContextObject from the file, and passes this context to Dump rather than the usual <font
color="#008000">afxDump().</font></pre>
<P>If you run this program, you will see output like that in Figure C.12. The file <B>dumpout.txt</B> will contain these lines:</P>
<pre><font color="#008000">a CObject at $71FDE4</font></pre>
<pre><font color="#008000">Kate Gregory</font></pre>
<A HREF="GGfig12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxc/GGfig12.gif"><b>Fig. C.12</b></A>
<P><I>Using the afxDump context sends your output to the Debug window.</I></P>
<P>The first line of the output, both to the debug window and to the file, came from <font color="#008000">CObject::Dump()</font> and gives you the object type and the address. The second line is from your own code and is simply the <font
color="#008000">CString</font> kept within each <font color="#008000">CPeople</font>.</P>
<P>If you get error messages when linking a Debug version of this product that refer to <font color="#008000">_beginthreadex</font> and <font color="#008000">_endthreadex</font>, you need to change some settings. By default, console applications are
single-threaded, but MFC is multi-threaded. By including afx.h and bringing in MFC, this application is making itself incompatible with the single-threaded default. To fix this, choose Project Settings and click the C/C++ tab. From the drop-down box at the
top of the dialog box, choose Code Generation. In the drop-down box labelled <font color="#008000">Use Runtime Library</font>, choose Debug Multithreaded. (The completed dialog is shown in Figure C.13.) Click OK, and rebuild the project. You should usually
change the settings for Release as well, but since the calls to <font color="#008000">Dump()</font> aren't surrounded by tests of _DEBUG, this code won't compile a Release version anyway.</P>
<A HREF="GGfig13.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxc/GGfig13.gif"><b>Fig. C.13</b></A>
<P><I>To use MFC in a console application, change to the multithreaded runtime </I><I>library.</I></P>
<P>Now that you've seen the basic tools of debugging in action, you'll be ready to put them to work in your own applications. You'll find errors quickly, understand other people's code, and see with your own eyes just how message-routing and other
behind-the-scenes magic really occurs. If you find yourself actually enjoying debugging, don't worry: no one else has to know!</P>
<p><hr></p>
<center>
<p><font size=-2>
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a
Simon and Schuster Company.</font></p>
</center>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -