{"id":191,"date":"2009-09-24T15:31:35","date_gmt":"2009-09-24T07:31:35","guid":{"rendered":"http:\/\/www.genepeng.com\/index.php\/191"},"modified":"2009-10-23T11:09:25","modified_gmt":"2009-10-23T03:09:25","slug":"c-minute","status":"publish","type":"post","link":"https:\/\/www.genepeng.com\/index.php\/191","title":{"rendered":"c++ \u5b66\u4e60\u7b14\u8bb0"},"content":{"rendered":"<ul>\n<li><font color=\"#0000ff\">1. Never Return a Pointer to a Local Object <\/font><\/li>\n<li><font color=\"#0000ff\">2. Function Declarations Go in Header Files<\/font> <\/li>\n<\/ul>\n<p>It may be temptingand would be legalto put a function declaration directly in each source file that uses the function. The problem with this approach is that it is tedious and error-prone. By putting function declarations into header files, we can ensure that all the declarations for a given function agree. If the interface to the function changes, only one declaration must be changed.<\/p>\n<ul>\n<li><font color=\"#0000ff\">3. Constraints on Specifying Default Arguments<\/font> <\/li>\n<\/ul>\n<p>Specify default argument(s) in either the function definition or declaration. However, a parameter can have its default argument specified only once in a file. Default arguments ordinarily should be specified with the declaration for the function and placed in an appropriate header<\/p>\n<ul>\n<li><font color=\"#0000ff\">4. Unlike other function definitions, inlines should be defined in header files. <\/font><\/li>\n<li><font color=\"#0000ff\">5. Functions cannot be overloaded based only on differences in the return type. <\/font><\/li>\n<li><font color=\"#0000ff\">6. It is a bad idea to declare a function locally. Function declarations should go in header files.<\/font> <\/li>\n<\/ul>\n<p>If we declare a function locally, that function hides rather than overloads the same function declared in an outer scope. As a consequence, declarations for every version of an overloaded function must appear in the same scope.<\/p>\n<pre style=\"border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 461px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 174px; background-color: #fbfbfb\"><pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\"><span style=\"color: #0000ff\">void<\/span> print(<span style=\"color: #0000ff\">const<\/span> <span style=\"color: #0000ff\">string<\/span> &amp;);\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\"><span style=\"color: #0000ff\">void<\/span> print(<span style=\"color: #0000ff\">double<\/span>);   <span style=\"color: #008000\">\/\/ overloads the print function<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\"><span style=\"color: #0000ff\">void<\/span> fooBar(<span style=\"color: #0000ff\">int<\/span> ival)\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">{\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">  <span style=\"color: #0000ff\">void<\/span> print(<span style=\"color: #0000ff\">int<\/span>);   <span style=\"color: #008000\">\/\/ new scope: hides previous instances of print<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">  print(&quot;<span style=\"color: #8b0000\">Value: <\/span>&quot;);  <span style=\"color: #008000\">\/\/ error: print(const string &amp;) is hidden<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">  print(ival); <span style=\"color: #008000\">\/\/ ok: print(int) is visible<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">  print(3.14); <span style=\"color: #008000\">\/\/ ok: calls print(int); print(double) is hidden<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">}\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\"><\/pre>\n<ul>\n<li><font color=\"#0000ff\">7. No Copy or Assign for IO Objects<\/font> <\/li>\n<li>cannot have a <tt>vector<\/tt> (or other container) that holds stream objects. <\/li>\n<li>cannot have a parameter or return type that is one of the stream types. If we need to pass or return an IO object, it must be passed or returned as a pointer or reference: <\/li>\n<li><font color=\"#0000ff\">8. IO =&gt; Buffers Are Not Flushed if the Program Crashes<\/font> <\/li>\n<\/ul>\n<p>Flushing the Output Buffer <\/p>\n<pre style=\"border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 459px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 59px; background-color: #fbfbfb\"><pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">cout &lt;&lt; &quot;<span style=\"color: #8b0000\">hi!<\/span>&quot; &lt;&lt; flush;      <span style=\"color: #008000\">\/\/ flushes the buffer; adds no data<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">cout &lt;&lt; &quot;<span style=\"color: #8b0000\">hi!<\/span>&quot; &lt;&lt; ends;       <span style=\"color: #008000\">\/\/ inserts a null, then flushes the buffer<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">cout &lt;&lt; &quot;<span style=\"color: #8b0000\">hi!<\/span>&quot; &lt;&lt; endl;       <span style=\"color: #008000\">\/\/ inserts a newline, then flushes the buffer<\/span><\/pre>\n<ul>\n<li><font color=\"#0000ff\">9. When we copy one container into another, the types must match exactly: The container type and element type must be the same.<\/font> <\/li>\n<li><font color=\"#0000ff\">10. Containers of Containers<\/font> <\/li>\n<\/ul>\n<pre style=\"border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 460px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 43px; background-color: #fbfbfb\"><pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">vector&lt; vector&lt;<span style=\"color: #0000ff\">string<\/span>&gt; &gt; lines; <span style=\"color: #008000\">\/\/ ok: space required between close &gt;<\/span>\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">vector&lt; vector&lt;<span style=\"color: #0000ff\">string<\/span>&gt;&gt; lines; <span style=\"color: #008000\">\/\/ error: &gt;&gt; treated as shift operator<\/span><\/pre>\n<\/p>\n<ul>\n<li><font color=\"#0000ff\">11. <tt>&gt;, &gt;=, &lt;, &lt;= <\/tt>Supported only for <tt>vector<\/tt> and <tt>deque\u2018s iterator<\/tt>.<\/font> <\/li>\n<\/ul>\n<p>The reason that only <tt>vector<\/tt> and <tt>deque<\/tt> support the relational operators is that only <tt>vector<\/tt> and <tt>deque<\/tt> offer fast, random access to their elements. These containers are guaranteed to let us efficiently jump directly to an element given its position in the container. Because these containers support random access by position, it is possible for their iterators to efficiently implement the arithmetic and relational operations.<\/p>\n<ul>\n<li><font color=\"#0000ff\">12. Container Elements Are Copies<\/font> <\/li>\n<\/ul>\n<p>When we add an element to a container, we do so by copying the element value into the container. Similarly, when we initialize a container by providing a range of elements, the new container contains copies of the original range of elements. There is no relationship between the element in the container and the value from which it was copied. Subsequent changes to the element in the container have no effect on the value that was copied, and vice versa.<\/p>\n<ul>\n<li><font color=\"#0000ff\">13. we can think of a constructor as executing in two phases: (1) the initialization phase and (2) a general computation phase<\/font>\n<pre style=\"border-right: #cecece 1px solid; padding-right: 5px; border-top: #cecece 1px solid; padding-left: 5px; min-height: 40px; padding-bottom: 5px; overflow: auto; border-left: #cecece 1px solid; width: 448px; padding-top: 5px; border-bottom: #cecece 1px solid; height: 126px; background-color: #fbfbfb\"><pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">     Sales_item::Sales_item(<span style=\"color: #0000ff\">const<\/span> <span style=\"color: #0000ff\">string<\/span> &amp;book)\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">     {\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">         isbn = book;\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">         units_sold = 0;\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #fbfbfb\">         revenue = 0.0;\n<\/pre>\n<pre style=\"font-size: 10px; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; background-color: #ffffff\">     }<\/pre>\n<\/li>\n<\/ul>\n<ul><\/ul>\n<p>This constructor assigns, but does not explicitly initialize, the members of class <tt>Sales_item<\/tt>. Regardless of the lack of an explicit initializer, the <tt>isbn<\/tt> member is initialized before the constructor is executed. This constructor implicitly uses the default <tt>string<\/tt> constructor to initialize <tt>isbn<\/tt>. When the body of the constructor is executed, the <tt>isbn<\/tt> member already has a value. That value is overwritten by the assignment inside the constructor body.<\/p>\n<ul>\n<li><font color=\"#0000ff\">14. \u4e0d\u80fd\u5728\u7c7b\u58f0\u660e\u4e2d\u521d\u59cb\u5316const\u6570\u636e\u6210\u5458\u3002\u4ee5\u4e0b\u7528\u6cd5\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u7c7b\u7684\u5bf9\u8c61\u672a\u88ab\u521b\u5efa\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053SIZE\u7684\u503c\u662f\u4ec0\u4e48\u3002<\/font><\/li>\n<\/ul>\n<p>class A<br \/>\n  <br \/>{<\/p>\n<p>&#160;&#160;&#160; const int SIZE = 100;&#160;&#160;&#160;&#160; \/\/ \u9519\u8bef\uff0c\u4f01\u56fe\u5728\u7c7b\u58f0\u660e\u4e2d\u521d\u59cb\u5316const\u6570\u636e\u6210\u5458<\/p>\n<p>&#160;&#160;&#160; int array[SIZE];&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; \/\/ \u9519\u8bef\uff0c\u672a\u77e5\u7684SIZE<\/p>\n<p>}; <\/p>\n<p>const\u6570\u636e\u6210\u5458\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u7c7b\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u8868\u4e2d\u8fdb\u884c\uff0c\u4f8b\u5982 <\/p>\n<p>class A<br \/>\n  <br \/>{<\/p>\n<p>&#160;&#160;&#160; A(int size);&#160;&#160;&#160;&#160;&#160; \/\/ \u6784\u9020\u51fd\u6570<\/p>\n<p>&#160;&#160;&#160; const int SIZE ; <\/p>\n<p>}; <\/p>\n<p>A::A(int size) : SIZE(size)&#160;&#160;&#160; \/\/ \u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u8868<br \/>\n  <br \/>{<\/p>\n<p>&#160;&#160;&#160; \u2026 <\/p>\n<p>} <\/p>\n<p>A&#160; a(100); \/\/ \u5bf9\u8c61 a \u7684SIZE\u503c\u4e3a100<br \/>\n  <br \/>A&#160; b(200); \/\/ \u5bf9\u8c61 b \u7684SIZE\u503c\u4e3a200 <\/p>\n<p>\u600e\u6837\u624d\u80fd\u5efa\u7acb\u5728\u6574\u4e2a\u7c7b\u4e2d\u90fd\u6052\u5b9a\u7684\u5e38\u91cf\u5462\uff1f\u522b\u6307\u671bconst\u6570\u636e\u6210\u5458\u4e86\uff0c\u5e94\u8be5\u7528\u7c7b\u4e2d\u7684\u679a\u4e3e\u5e38\u91cf\u6765\u5b9e\u73b0\u3002\u4f8b\u5982 <\/p>\n<p>class A<br \/>\n  <br \/>{\u2026<\/p>\n<p>&#160;&#160;&#160; enum { SIZE1 = 100, SIZE2 = 200}; \/\/ \u679a\u4e3e\u5e38\u91cf<\/p>\n<p>&#160;&#160;&#160; int array1[SIZE1];&#160; <br \/>&#160;&#160;&#160; int array2[SIZE2];<\/p>\n<p>&#160;&#160;&#160; }; <\/p>\n<p>\u679a\u4e3e\u5e38\u91cf\u4e0d\u4f1a\u5360\u7528\u5bf9\u8c61\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5b83\u4eec\u5728\u7f16\u8bd1\u65f6\u88ab\u5168\u90e8\u6c42\u503c\u3002\u679a\u4e3e\u5e38\u91cf\u7684\u7f3a\u70b9\u662f\uff1a\u5b83\u7684\u9690\u542b\u6570\u636e\u7c7b\u578b\u662f\u6574\u6570\uff0c\u5176\u6700\u5927\u503c\u6709\u9650\uff0c\u4e14\u4e0d\u80fd\u8868\u793a\u6d6e\u70b9\u6570\uff08\u5982PI=3.14159\uff09\u3002 <\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Never Return a Pointer to a Local Object 2. Function Declarations Go in Header Files It may be temptingand would be legalto put a function declaration directly in each source file that uses the function. The problem with this approach is that it is tedious and error-prone. By putting function declarations into header files, &#8230; <a title=\"c++ \u5b66\u4e60\u7b14\u8bb0\" class=\"read-more\" href=\"https:\/\/www.genepeng.com\/index.php\/191\" aria-label=\"More on c++ \u5b66\u4e60\u7b14\u8bb0\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[5],"tags":[],"_links":{"self":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/191"}],"collection":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":23,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"predecessor-version":[{"id":224,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/posts\/191\/revisions\/224"}],"wp:attachment":[{"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.genepeng.com\/index.php\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}