⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch24.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 3 页
字号:

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P><I>Listing 24.2&#151;A Scaled-Down Version of the </I>auto_ptr<I> Class</I></P>

<pre><font color="#008000">  // This class is not complete. Use the complete definition in </font></pre>

<pre><font color="#008000">  //the Standard Template Library.</font></pre>

<pre><font color="#008000">   template &lt;class T&gt;</font></pre>

<pre><font color="#008000">   class auto_ptr</font></pre>

<pre><font color="#008000">   {</font></pre>

<pre><font color="#008000">   public:</font></pre>

<pre><font color="#008000">           auto_ptr( T *p = 0) : rep(p) {} </font></pre>

<pre><font color="#008000">          // store pointer in the class</font></pre>

<pre><font color="#008000">          ~auto_ptr(){ delete rep; }            // delete internal rep</font></pre>

<pre><font color="#008000">          // include pointer conversion members</font></pre>

<pre><font color="#008000">          inline T* operator-&gt;() const { return rep; }</font></pre>

<pre><font color="#008000">          inline T&amp; operator*() const { return *rep; }</font></pre>

<pre><font color="#008000">    private:</font></pre>

<pre><font color="#008000">          T * rep;</font></pre>

<pre><font color="#008000">   };</font></pre>

<P>The class has one member variable, a pointer to whatever type it is you want a pointer to. It has a one argument constructor to build an <font color="#008000">auto_ptr</font> from a <font color="#008000">int*</font> or a <font 
color="#008000">Truck*</font> or any other pointer type. The destructor <font color="#008000">delete</font>s the memory pointed to by the internal member variable. Finally, the class overrides -&gt; and *, the dereferencing operators, so that dereferencing 
an <font color="#008000">auto_ptr</font> feels just like dereferencing an ordinary pointer.</P>

<P>If there is some class <font color="#008000">C</font> to which you want to make an automatic pointer called <font color="#008000">p</font>, all you do is this:</P>

<pre><font color="#008000">auto_ptr&lt;C&gt; p(new C());</font></pre>

<P>Now you can use <font color="#008000">p</font> just as though it was a <font color="#008000">C*</font>. For example:</P>

<pre><font color="#008000">p-&gt;Method();   // calls C::Method()</font></pre>

<P>You never have to <font color="#008000">delete</font> the <font color="#008000">C</font> object that <font color="#008000">p</font> points to, even in the event of an exception, because <font color="#008000">p</font> was allocated on the stack. When it 
goes out of scope, its destructor is called and the destructor calls <font color="#008000">delete</font> on the <font color="#008000">C</font> object that was allocated in the <font color="#008000">new</font> statement.</P>

<H3><A ID="I11" NAME="I11"><B>Optimization</B></A></H3>

<P>There was a time when programmers were expected to optimize their code themselves. Many a night was spent arguing about the order in which to test conditions, or which variables should be register rather than automatic storage. These days, compilers 
come with optimizers that can speed execution or shrink program size far beyond what a typical programmer can accomplish by hand.</P>

<P>Here&#146;s a simple example of how optimizers work. Imagine you have written a piece of code like this:</P>

<pre><font color="#008000">for (i=0;i&lt;10;i++)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    y=2;</font></pre>

<pre><font color="#008000">    x[i]=5;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">for (i=0; i&lt;10; i++)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    total += x[i];</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Your code will run faster, with no impact on the final results, if the <font color="#008000">y=2</font> is moved to before the first loop. In addition, the two loops can easily be combined into a single loop. If you do that, it&#146;s faster to add 
<font color="#008000">5</font> to total each time than it is to calculate the address of <font color="#008000">x[i]</font> in order to retrieve the value just stored into it. Really bright optimizers may even realize that total can be calculated outside 
the loop as well. The revised code may look like this:</P>

<pre><font color="#008000">y=2;</font></pre>

<pre><font color="#008000">for (i=0;i&lt;10;i++)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    x[i]=5;</font></pre>

<pre><font color="#008000">}</font></pre>

<pre><font color="#008000">    total += 50;</font></pre>

<P>Optimizers do far more than this, of course, but this example gives you an idea of what&#146;s going on behind the scenes. It&#146;s up to you whether the optimizer focuses on speed, occasionally at the expense of memory usage, or tries to minimize 
memory usage, perhaps at a slighter lower speed.</P>

<P>To set the optimization options for your project, select the <U>P</U>roject, <U>S</U>ettings command from Developer Studio's menu bar. The Project Settings property sheet, first shown in Figure 24.1, appears. Click the C/C++ tab and make sure you are 
looking at the Release settings, then select Optimizations in the Categor<U>y</U> box. Optimization should be turned off for debug builds, since the code in your source files and the code being executed won&#146;t match line for line, which will confuse 
you and the debugger. You should turn on some kind of optimization for release builds. Choose from the drop-down list box, as shown in Figure 24.5.</P>

<A HREF="Yfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch24/Yfigs05.gif"><b>Fig. 24.5</b></A>

<P><I>Select the type of optimization you want.</I></P>

<P>If you select the Customize option in the Optimi<U>z</U>ations box, you can select from the list of individual optimizations, including Assume No Aliasing, Global Optimizations, Favor Fast Code, Generate Intrinsic Functions, Frame-Pointer Omission, and 
more. However, as you can tell from the names of these optimizations, you really have to know what you're doing before you set up a custom optimization scheme. For now, accept the schemes that have been laid out for you.</P>

<H3><A ID="I12" NAME="I12"><B>Profiling</B></A></H3>

<P>Profiling an application lets you discover bottlenecks, pieces of code that are slowing your application&#146;s execution and deserve special attention. It&#146;s pointless to hand-optimize a routine if you don&#146;t know that the routine is called 
often enough for its speed to matter.</P>

<P>Another use of a profiler is to see whether the test cases you have put together result in every one of your functions being called, or in each line of your code being executed. You may think you have selected test inputs that guarantee this; however, 
the profiler can confirm it for you.</P>

<P>Visual C++ includes a profiler integrated with the IDE: all you need to do is use it. First, adjust your project settings to include profiler information. Bring up the Project Settings property sheet as you did in the preceeding section and click the 
Link tab. Check the Enable Profiling check box. Click OK and rebuild your project. Links will be slower now, because you cannot do an incremental link when you are planning to profile, but you can go back to your old settings once you&#146;ve learned a 
little about the way your program runs. Choose <U>B</U>uild, <U>P</U>rofile, and the Profile dialog box, shown in Figure 24.6, appears.</P>

<A HREF="Yfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch24/Yfigs06.gif"><b>Fig. 24.6</b></A>

<P><I>A profiler can gather many kinds of information.</I></P>

<P>If you aren&#146;t sure what any of the radio buttons on this dialog box mean, click the ? (question mark) in the upper-right corner and then click the radio button. You will receive a short explanation of the option. (If you would like to add this 
kind of context-sensitive help to your own applications, be sure to read <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, &#147;Help.&#148;)</P>

<P>You don&#146;t profile as a way to catch bugs, but it can help to validate your testing, or show you the parts of your application that need work, which makes it a vital part of the developer&#146;s toolbox. Get in the habit of profiling all of your 
applications at least once in the development cycle.</P>

<H3><A ID="I13" NAME="I13"><B>From Here</B></A><B>...</B></H3>

<P>MFC provides you with powerful tools to help ensure that your programs are bug-free and fast. <font color="#008000">ASSERT</font> statements protect you against errors in logic. <font color="#008000">TRACE</font> statements let you follow your 
application&#146;s progress without having to use the debugger. The debug <font color="#008000">new</font> and <font color="#008000">delete</font> make memory leaks easier to track. All of these features disappear in release builds, which means they cannot 
slow down or bloat the code your users get&#151;though the impact of using these techniques in development can be a significantly positive one. Finally, the optimizer can speed or shrink your code, and the profiler can draw your attention to specific 
functions that need attention. Programmers who use the techniques presented in this chapter will produce faster, neater code with less bugs and no mysterious hangs from memory leaks.</P>

<P>To learn about other techniques and tools, see these chapters:</P>

<ul>

<li> <A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Building a Complete Application: ShowString,&quot; provides the information you need to build menus, dialog boxes, and other user interface elements.</P>

<li> <A HREF="index11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index11.htm" target="text">Chapter 11</A>, &#147;Help,&#148; shows you how to add Help to your application.</P>

<li> <A HREF="index26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index26.htm" target="text">Chapter 26</A>, &#147;Exceptions, Templates, and the Latest Additions to C++&#148; discusses exceptions and templates.</P>

</ul>

<p><hr></p>

<center>

<p><font size=-2>

&copy; 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 + -