{"id":628,"date":"2017-07-27T15:43:02","date_gmt":"2017-07-27T07:43:02","guid":{"rendered":"http:\/\/williamxin.f3322.org\/wordpress\/?p=195"},"modified":"2017-11-24T15:19:07","modified_gmt":"2017-11-24T07:19:07","slug":"google-c%e7%bc%96%e7%a8%8b%e9%a3%8e%e6%a0%bc%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/i007.cc\/wordpress\/archives\/628","title":{"rendered":"Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357"},"content":{"rendered":"<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/10\/55818.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e00\uff09<\/a><\/h1>\n<h2>\u80cc\u666f<\/h2>\n<p>Google\u7684\u5f00\u6e90\u9879\u76ee\u5927\u591a\u4f7f\u7528C++\u5f00\u53d1\u3002\u6bcf\u4e00\u4e2aC++\u7a0b\u5e8f\u5458\u4e5f\u90fd\u77e5\u9053\uff0cC++\u5177\u6709\u5f88\u591a\u5f3a\u5927\u7684\u8bed\u8a00\u7279\u6027\uff0c\u4f46\u8fd9\u79cd\u5f3a\u5927\u4e0d\u53ef\u907f\u514d\u7684\u5bfc\u81f4\u5b83\u7684\u590d\u6742\uff0c\u8fd9\u79cd\u590d\u6742\u4f1a\u4f7f\u5f97\u4ee3\u7801\u66f4\u6613\u4e8e\u51fa\u73b0bug\u3001\u96be\u4e8e\u9605\u8bfb\u548c\u7ef4\u62a4\u3002<\/p>\n<p>\u672c\u6307\u5357\u7684\u76ee\u7684\u662f\u901a\u8fc7\u8be6\u7ec6\u9610\u8ff0\u5728C++\u7f16\u7801\u65f6\u8981\u600e\u6837\u5199\u3001\u4e0d\u8981\u600e\u6837\u5199\u6765\u89c4\u907f\u5176\u590d\u6742\u6027\u3002\u8fd9\u4e9b\u89c4\u5219\u53ef\u5728\u5141\u8bb8\u4ee3\u7801\u6709\u6548\u4f7f\u7528C++\u8bed\u8a00\u7279\u6027\u7684\u540c\u65f6\u4f7f\u5176\u6613\u4e8e\u7ba1\u7406\u3002<\/p>\n<p>\u98ce\u683c\uff0c\u4e5f\u88ab\u89c6\u4e3a\u53ef\u8bfb\u6027\uff0c\u4e3b\u8981\u6307\u79f0\u7ba1\u7406C++\u4ee3\u7801\u7684\u4e60\u60ef\u3002\u4f7f\u7528\u672f\u8bed\u98ce\u683c\u6709\u70b9\u7528\u8bcd\u4e0d\u5f53\uff0c\u56e0\u4e3a\u8fd9\u4e9b\u4e60\u60ef\u8fdc\u4e0d\u6b62\u6e90\u4ee3\u7801\u6587\u4ef6\u683c\u5f0f\u8fd9\u4e48\u7b80\u5355\u3002<\/p>\n<p>\u4f7f\u4ee3\u7801\u6613\u4e8e\u7ba1\u7406\u7684\u65b9\u6cd5\u4e4b\u4e00\u662f\u589e\u5f3a\u4ee3\u7801\u4e00\u81f4\u6027\uff0c\u8ba9\u522b\u4eba\u53ef\u4ee5\u8bfb\u61c2\u4f60\u7684\u4ee3\u7801\u662f\u5f88\u91cd\u8981\u7684\uff0c\u4fdd\u6301\u7edf\u4e00\u7f16\u7a0b\u98ce\u683c\u610f\u5473\u7740\u53ef\u4ee5\u8f7b\u677e\u6839\u636e\u201c\u6a21\u5f0f\u5339\u914d\u201d\u89c4\u5219\u63a8\u65ad\u5404\u79cd\u7b26\u53f7\u7684\u542b\u4e49\u3002\u521b\u5efa\u901a\u7528\u7684\u3001\u5fc5\u9700\u7684\u4e60\u60ef\u7528\u8bed\u548c\u6a21\u5f0f\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u52a0\u5bb9\u6613\u7406\u89e3\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u6539\u53d8\u4e00\u4e9b\u7f16\u7a0b\u98ce\u683c\u53ef\u80fd\u4f1a\u662f\u597d\u7684\u9009\u62e9\uff0c\u4f46\u6211\u4eec\u8fd8\u662f\u5e94\u8be5\u9075\u5faa\u4e00\u81f4\u6027\u539f\u5219\uff0c\u5c3d\u91cf\u4e0d\u8fd9\u6837\u53bb\u505a\u3002<\/p>\n<p>\u672c\u6307\u5357\u7684\u53e6\u4e00\u4e2a\u89c2\u70b9\u662fC++\u7279\u6027\u7684\u81c3\u80bf\u3002C++\u662f\u4e00\u95e8\u5305\u542b\u5927\u91cf\u9ad8\u7ea7\u7279\u6027\u7684\u5de8\u578b\u8bed\u8a00\uff0c\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u4f1a\u9650\u5236\u751a\u81f3\u7981\u6b62\u4f7f\u7528\u67d0\u4e9b\u7279\u6027\u4f7f\u4ee3\u7801\u7b80\u5316\uff0c\u907f\u514d\u53ef\u80fd\u5bfc\u81f4\u7684\u5404\u79cd\u95ee\u9898\uff0c\u6307\u5357\u4e2d\u5217\u4e3e\u4e86\u8fd9\u7c7b\u7279\u6027\uff0c\u5e76\u89e3\u91ca\u8bf4\u4e3a\u4ec0\u4e48\u8fd9\u4e9b\u7279\u6027\u662f\u88ab\u9650\u5236\u4f7f\u7528\u7684\u3002<\/p>\n<p>\u7531Google\u5f00\u53d1\u7684\u5f00\u6e90\u9879\u76ee\u5c06\u9075\u7167\u672c\u6307\u5357\u7ea6\u5b9a\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u672c\u6307\u5357\u5e76\u975eC++\u6559\u7a0b\uff0c\u6211\u4eec\u5047\u5b9a\u8bfb\u8005\u5df2\u7ecf\u5bf9C++\u975e\u5e38\u719f\u6089\u3002<\/p>\n<h2>\u5934\u6587\u4ef6<\/h2>\n<p>\u901a\u5e38\uff0c\u6bcf\u4e00\u4e2a.cc\u6587\u4ef6\uff08C++\u7684\u6e90\u6587\u4ef6\uff09\u90fd\u6709\u4e00\u4e2a\u5bf9\u5e94\u7684.h\u6587\u4ef6\uff08\u5934\u6587\u4ef6\uff09\uff0c\u4e5f\u6709\u4e00\u4e9b\u4f8b\u5916\uff0c\u5982\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u548c\u53ea\u5305\u542bmain()\u7684.cc\u6587\u4ef6\u3002<\/p>\n<p>\u6b63\u786e\u4f7f\u7528\u5934\u6587\u4ef6\u53ef\u4ee4\u4ee3\u7801\u5728\u53ef\u8bfb\u6027\u3001\u6587\u4ef6\u5927\u5c0f\u548c\u6027\u80fd\u4e0a\u5927\u4e3a\u6539\u89c2\u3002<\/p>\n<p>\u4e0b\u9762\u7684\u89c4\u5219\u5c06\u5f15\u5bfc\u4f60\u89c4\u907f\u4f7f\u7528\u5934\u6587\u4ef6\u65f6\u7684\u5404\u79cd\u9ebb\u70e6\u3002<\/p>\n<h2>1. #define\u7684\u4fdd\u62a4<\/h2>\n<p>\u6240\u6709\u5934\u6587\u4ef6\u90fd\u5e94\u8be5\u4f7f\u7528#define\u9632\u6b62\u5934\u6587\u4ef6\u88ab\u591a\u91cd\u5305\u542b\uff08multiple inclusion\uff09\uff0c\u547d\u540d\u683c\u5f0f\u5f53\u662f\uff1a<em>&lt;PROJECT&gt;<\/em>_<em>&lt;PATH&gt;<\/em>_<em>&lt;FILE&gt;<\/em>_H_<\/p>\n<p>\u4e3a\u4fdd\u8bc1\u552f\u4e00\u6027\uff0c\u5934\u6587\u4ef6\u7684\u547d\u540d\u5e94\u57fa\u4e8e\u5176\u6240\u5728\u9879\u76ee\u6e90\u4ee3\u7801\u6811\u7684\u5168\u8def\u5f84\u3002\u4f8b\u5982\uff0c\u9879\u76eefoo\u4e2d\u7684\u5934\u6587\u4ef6foo\/src\/bar\/baz.h\u6309\u5982\u4e0b\u65b9\u5f0f\u4fdd\u62a4\uff1a<\/p>\n<p>#ifndef FOO_BAR_BAZ_H_<\/p>\n<p>#define FOO_BAR_BAZ_H_<\/p>\n<p>&#8230;<\/p>\n<p>#endif \/\/ FOO_BAR_BAZ_H_<\/p>\n<h2>2. \u5934\u6587\u4ef6\u4f9d\u8d56<\/h2>\n<p>\u4f7f\u7528\u524d\u7f6e\u58f0\u660e\uff08forward declarations\uff09\u5c3d\u91cf\u51cf\u5c11.h\u6587\u4ef6\u4e2d#include\u7684\u6570\u91cf\u3002<\/p>\n<p>\u5f53\u4e00\u4e2a\u5934\u6587\u4ef6\u88ab\u5305\u542b\u7684\u540c\u65f6\u4e5f\u5f15\u5165\u4e86\u4e00\u9879\u65b0\u7684\u4f9d\u8d56\uff08dependency\uff09\uff0c\u53ea\u8981\u8be5\u5934\u6587\u4ef6\u88ab\u4fee\u6539\uff0c\u4ee3\u7801\u5c31\u8981\u91cd\u65b0\u7f16\u8bd1\u3002\u5982\u679c\u4f60\u7684\u5934\u6587\u4ef6\u5305\u542b\u4e86\u5176\u4ed6\u5934\u6587\u4ef6\uff0c\u8fd9\u4e9b\u5934\u6587\u4ef6\u7684\u4efb\u4f55\u6539\u53d8\u4e5f\u5c06\u5bfc\u81f4\u90a3\u4e9b\u5305\u542b\u4e86\u4f60\u7684\u5934\u6587\u4ef6\u7684\u4ee3\u7801\u91cd\u65b0\u7f16\u8bd1\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5b81\u53ef\u5c3d\u91cf\u5c11\u5305\u542b\u5934\u6587\u4ef6\uff0c\u5c24\u5176\u662f\u90a3\u4e9b\u5305\u542b\u5728\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u7684\u3002<\/p>\n<p>\u4f7f\u7528\u524d\u7f6e\u58f0\u660e\u53ef\u4ee5\u663e\u8457\u51cf\u5c11\u9700\u8981\u5305\u542b\u7684\u5934\u6587\u4ef6\u6570\u91cf\u3002\u4e3e\u4f8b\u8bf4\u660e\uff1a\u5934\u6587\u4ef6\u4e2d\u7528\u5230\u7c7bFile\uff0c\u4f46\u4e0d\u9700\u8981\u8bbf\u95eeFile\u7684\u58f0\u660e\uff0c\u5219\u5934\u6587\u4ef6\u4e2d\u53ea\u9700\u524d\u7f6e\u58f0\u660eclass File;\u65e0\u9700#include &#8220;file\/base\/file.h&#8221;\u3002<\/p>\n<p>\u5728\u5934\u6587\u4ef6\u5982\u4f55\u505a\u5230\u4f7f\u7528\u7c7bFoo\u800c\u65e0\u9700\u8bbf\u95ee\u7c7b\u7684\u5b9a\u4e49\uff1f<\/p>\n<p>1) \u5c06\u6570\u636e\u6210\u5458\u7c7b\u578b\u58f0\u660e\u4e3aFoo *\u6216Foo &amp;\uff1b<\/p>\n<p>2) \u53c2\u6570\u3001\u8fd4\u56de\u503c\u7c7b\u578b\u4e3aFoo\u7684\u51fd\u6570\u53ea\u662f\u58f0\u660e\uff08\u4f46\u4e0d\u5b9a\u4e49\u5b9e\u73b0\uff09\uff1b<\/p>\n<p>3) \u9759\u6001\u6570\u636e\u6210\u5458\u7684\u7c7b\u578b\u53ef\u4ee5\u88ab\u58f0\u660e\u4e3aFoo\uff0c\u56e0\u4e3a\u9759\u6001\u6570\u636e\u6210\u5458\u7684\u5b9a\u4e49\u5728\u7c7b\u5b9a\u4e49\u4e4b\u5916\u3002<\/p>\n<p>\u53e6\u4e00\u65b9\u9762\uff0c\u5982\u679c\u4f60\u7684\u7c7b\u662fFoo\u7684\u5b50\u7c7b\uff0c\u6216\u8005\u542b\u6709\u7c7b\u578b\u4e3aFoo\u7684\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff0c\u5219\u5fc5\u987b\u4e3a\u4e4b\u5305\u542b\u5934\u6587\u4ef6\u3002<\/p>\n<p>\u6709\u65f6\uff0c\u4f7f\u7528\u6307\u9488\u6210\u5458\uff08pointer members\uff0c\u5982\u679c\u662fscoped_ptr\u66f4\u597d\uff09\u66ff\u4ee3\u5bf9\u8c61\u6210\u5458\uff08object members\uff09\u7684\u786e\u66f4\u6709\u610f\u4e49\u3002\u7136\u800c\uff0c\u8fd9\u6837\u7684\u505a\u6cd5\u4f1a\u964d\u4f4e\u4ee3\u7801\u53ef\u8bfb\u6027\u53ca\u6267\u884c\u6548\u7387\u3002\u5982\u679c\u4ec5\u4ec5\u4e3a\u4e86\u5c11\u5305\u542b\u5934\u6587\u4ef6\uff0c\u8fd8\u662f\u4e0d\u8981\u8fd9\u6837\u66ff\u4ee3\u7684\u597d\u3002<\/p>\n<p>\u5f53\u7136\uff0c.cc\u6587\u4ef6\u65e0\u8bba\u5982\u4f55\u90fd\u9700\u8981\u6240\u4f7f\u7528\u7c7b\u7684\u5b9a\u4e49\u90e8\u5206\uff0c\u81ea\u7136\u4e5f\u5c31\u4f1a\u5305\u542b\u82e5\u5e72\u5934\u6587\u4ef6\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u80fd\u4f9d\u8d56\u58f0\u660e\u7684\u5c31\u4e0d\u8981\u4f9d\u8d56\u5b9a\u4e49\u3002<\/strong><\/p>\n<h2>3. \u5185\u8054\u51fd\u6570<\/h2>\n<p>\u53ea\u6709\u5f53\u51fd\u6570\u53ea\u670910\u884c\u751a\u81f3\u66f4\u5c11\u65f6\u624d\u4f1a\u5c06\u5176\u5b9a\u4e49\u4e3a\u5185\u8054\u51fd\u6570\uff08inline function\uff09\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff08<\/strong><strong>Definition<\/strong><strong>\uff09\uff1a<\/strong>\u5f53\u51fd\u6570\u88ab\u58f0\u660e\u4e3a\u5185\u8054\u51fd\u6570\u4e4b\u540e\uff0c\u7f16\u8bd1\u5668\u53ef\u80fd\u4f1a\u5c06\u5176\u5185\u8054\u5c55\u5f00\uff0c\u65e0\u9700\u6309\u901a\u5e38\u7684\u51fd\u6570\u8c03\u7528\u673a\u5236\u8c03\u7528\u5185\u8054\u51fd\u6570\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u5f53\u51fd\u6570\u4f53\u6bd4\u8f83\u5c0f\u7684\u65f6\u5019\uff0c\u5185\u8054\u8be5\u51fd\u6570\u53ef\u4ee5\u4ee4\u76ee\u6807\u4ee3\u7801\u66f4\u52a0\u9ad8\u6548\u3002\u5bf9\u4e8e\u5b58\u53d6\u51fd\u6570\uff08accessor\u3001mutator\uff09\u4ee5\u53ca\u5176\u4ed6\u4e00\u4e9b\u6bd4\u8f83\u77ed\u7684\u5173\u952e\u6267\u884c\u51fd\u6570\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u6ee5\u7528\u5185\u8054\u5c06\u5bfc\u81f4\u7a0b\u5e8f\u53d8\u6162\uff0c\u5185\u8054\u6709\u53ef\u80fd\u662f\u76ee\u6807\u4ee3\u7801\u91cf\u6216\u589e\u6216\u51cf\uff0c\u8fd9\u53d6\u51b3\u4e8e\u88ab\u5185\u8054\u7684\u51fd\u6570\u7684\u5927\u5c0f\u3002\u5185\u8054\u8f83\u77ed\u5c0f\u7684\u5b58\u53d6\u51fd\u6570\u901a\u5e38\u4f1a\u51cf\u5c11\u4ee3\u7801\u91cf\uff0c\u4f46\u5185\u8054\u4e00\u4e2a\u5f88\u5927\u7684\u51fd\u6570\uff08\u8bd1\u8005\u6ce8\uff1a\u5982\u679c\u7f16\u8bd1\u5668\u5141\u8bb8\u7684\u8bdd\uff09\u5c06\u620f\u5267\u6027\u7684\u589e\u52a0\u4ee3\u7801\u91cf\u3002\u5728\u73b0\u4ee3\u5904\u7406\u5668\u4e0a\uff0c\u7531\u4e8e\u66f4\u597d\u7684\u5229\u7528\u6307\u4ee4\u7f13\u5b58\uff08instruction cache\uff09\uff0c\u5c0f\u5de7\u7684\u4ee3\u7801\u5f80\u5f80\u6267\u884c\u66f4\u5feb\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u4e00\u4e2a\u6bd4\u8f83\u5f97\u5f53\u7684\u5904\u7406\u89c4\u5219\u662f\uff0c\u4e0d\u8981\u5185\u8054\u8d85\u8fc710\u884c\u7684\u51fd\u6570\u3002\u5bf9\u4e8e\u6790\u6784\u51fd\u6570\u5e94\u614e\u91cd\u5bf9\u5f85\uff0c\u6790\u6784\u51fd\u6570\u5f80\u5f80\u6bd4\u5176\u8868\u9762\u770b\u8d77\u6765\u8981\u957f\uff0c\u56e0\u4e3a\u6709\u4e00\u4e9b\u9690\u5f0f\u6210\u5458\u548c\u57fa\u7c7b\u6790\u6784\u51fd\u6570\uff08\u5982\u679c\u6709\u7684\u8bdd\uff09\u88ab\u8c03\u7528\uff01<\/p>\n<p>\u53e6\u4e00\u6709\u7528\u7684\u5904\u7406\u89c4\u5219\uff1a\u5185\u8054\u90a3\u4e9b\u5305\u542b\u5faa\u73af\u6216switch\u8bed\u53e5\u7684\u51fd\u6570\u662f\u5f97\u4e0d\u507f\u5931\u7684\uff0c\u9664\u975e\u5728\u5927\u591a\u6570\u60c5\u51b5\u4e0b\uff0c\u8fd9\u4e9b\u5faa\u73af\u6216switch\u8bed\u53e5\u4ece\u4e0d\u6267\u884c\u3002<\/p>\n<p>\u91cd\u8981\u7684\u662f\uff0c\u865a\u51fd\u6570\u548c\u9012\u5f52\u51fd\u6570\u5373\u4f7f\u88ab\u58f0\u660e\u4e3a\u5185\u8054\u7684\u4e5f\u4e0d\u4e00\u5b9a\u5c31\u662f\u5185\u8054\u51fd\u6570\u3002\u901a\u5e38\uff0c\u9012\u5f52\u51fd\u6570\u4e0d\u5e94\u8be5\u88ab\u58f0\u660e\u4e3a\u5185\u8054\u7684\uff08\u8bd1\u8005\u6ce8\uff1a\u9012\u5f52\u8c03\u7528\u5806\u6808\u7684\u5c55\u5f00\u5e76\u4e0d\u50cf\u5faa\u73af\u90a3\u4e48\u7b80\u5355\uff0c\u6bd4\u5982\u9012\u5f52\u5c42\u6570\u5728\u7f16\u8bd1\u65f6\u53ef\u80fd\u662f\u672a\u77e5\u7684\uff0c\u5927\u591a\u6570\u7f16\u8bd1\u5668\u90fd\u4e0d\u652f\u6301\u5185\u8054\u9012\u5f52\u51fd\u6570\uff09\u3002\u6790\u6784\u51fd\u6570\u5185\u8054\u7684\u4e3b\u8981\u539f\u56e0\u662f\u5176\u5b9a\u4e49\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\uff0c\u4e3a\u4e86\u65b9\u4fbf\u6291\u6216\u662f\u5bf9\u5176\u884c\u4e3a\u7ed9\u51fa\u6587\u6863\u3002<\/p>\n<h2>4. -inl.h\u6587\u4ef6<\/h2>\n<p>\u590d\u6742\u7684\u5185\u8054\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u5e94\u653e\u5728\u540e\u7f00\u540d\u4e3a-inl.h\u7684\u5934\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u5728\u5934\u6587\u4ef6\u4e2d\u7ed9\u51fa\u5185\u8054\u51fd\u6570\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee4\u7f16\u8bd1\u5668\u5c06\u5176\u5728\u8c03\u7528\u5904\u5185\u8054\u5c55\u5f00\u3002\u7136\u800c\uff0c\u5b9e\u73b0\u4ee3\u7801\u5e94\u5b8c\u5168\u653e\u5230.cc\u6587\u4ef6\u4e2d\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b.h\u6587\u4ef6\u4e2d\u51fa\u73b0\u592a\u591a\u5b9e\u73b0\u4ee3\u7801\uff0c\u9664\u975e\u8fd9\u6837\u505a\u5728\u53ef\u8bfb\u6027\u548c\u6548\u7387\u4e0a\u6709\u660e\u663e\u4f18\u52bf\u3002<\/p>\n<p>\u5982\u679c\u5185\u8054\u51fd\u6570\u7684\u5b9a\u4e49\u6bd4\u8f83\u77ed\u5c0f\u3001\u903b\u8f91\u6bd4\u8f83\u7b80\u5355\uff0c\u5176\u5b9e\u73b0\u4ee3\u7801\u53ef\u4ee5\u653e\u5728.h\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff0c\u5b58\u53d6\u51fd\u6570\u7684\u5b9e\u73b0\u7406\u6240\u5f53\u7136\u90fd\u653e\u5728\u7c7b\u5b9a\u4e49\u4e2d\u3002\u51fa\u4e8e\u5b9e\u73b0\u548c\u8c03\u7528\u7684\u65b9\u4fbf\uff0c\u8f83\u590d\u6742\u7684\u5185\u8054\u51fd\u6570\u4e5f\u53ef\u4ee5\u653e\u5230.h\u6587\u4ef6\u4e2d\uff0c\u5982\u679c\u4f60\u89c9\u5f97\u8fd9\u6837\u4f1a\u4f7f\u5934\u6587\u4ef6\u663e\u5f97\u7b28\u91cd\uff0c\u8fd8\u53ef\u4ee5\u5c06\u5176\u5206\u79bb\u5230\u5355\u72ec\u7684-inl.h\u4e2d\u3002\u8fd9\u6837\u5373\u628a\u5b9e\u73b0\u548c\u7c7b\u5b9a\u4e49\u5206\u79bb\u5f00\u6765\uff0c\u5f53\u9700\u8981\u65f6\u5305\u542b\u5b9e\u73b0\u6240\u5728\u7684-inl.h\u5373\u53ef\u3002<\/p>\n<p>-inl.h\u6587\u4ef6\u8fd8\u53ef\u7528\u4e8e\u51fd\u6570\u6a21\u677f\u7684\u5b9a\u4e49\uff0c\u4ece\u800c\u4f7f\u5f97\u6a21\u677f\u5b9a\u4e49\u53ef\u8bfb\u6027\u589e\u5f3a\u3002<\/p>\n<p>\u8981\u63d0\u9192\u7684\u4e00\u70b9\u662f\uff0c-inl.h\u548c\u5176\u4ed6\u5934\u6587\u4ef6\u4e00\u6837\uff0c\u4e5f\u9700\u8981#define\u4fdd\u62a4\u3002<\/p>\n<h2>5. \u51fd\u6570\u53c2\u6570\u987a\u5e8f\uff08Function Parameter Ordering\uff09<\/h2>\n<p>\u5b9a\u4e49\u51fd\u6570\u65f6\uff0c\u53c2\u6570\u987a\u5e8f\u4e3a\uff1a\u8f93\u5165\u53c2\u6570\u5728\u524d\uff0c\u8f93\u51fa\u53c2\u6570\u5728\u540e\u3002<\/p>\n<p>C\/C++\u51fd\u6570\u53c2\u6570\u5206\u4e3a\u8f93\u5165\u53c2\u6570\u548c\u8f93\u51fa\u53c2\u6570\u4e24\u79cd\uff0c\u6709\u65f6\u8f93\u5165\u53c2\u6570\u4e5f\u4f1a\u8f93\u51fa\uff08\u8bd1\u8005\u6ce8\uff1a\u503c\u88ab\u4fee\u6539\u65f6\uff09\u3002\u8f93\u5165\u53c2\u6570\u4e00\u822c\u4f20\u503c\u6216\u5e38\u6570\u5f15\u7528\uff08const references\uff09\uff0c\u8f93\u51fa\u53c2\u6570\u6216\u8f93\u5165\/\u8f93\u51fa\u53c2\u6570\u4e3a\u975e\u5e38\u6570\u6307\u9488\uff08non-const pointers\uff09\u3002\u5bf9\u53c2\u6570\u6392\u5e8f\u65f6\uff0c\u5c06\u6240\u6709\u8f93\u5165\u53c2\u6570\u7f6e\u4e8e\u8f93\u51fa\u53c2\u6570\u4e4b\u524d\u3002\u4e0d\u8981\u4ec5\u4ec5\u56e0\u4e3a\u662f\u65b0\u6dfb\u52a0\u7684\u53c2\u6570\uff0c\u5c31\u5c06\u5176\u7f6e\u4e8e\u6700\u540e\uff0c\u800c\u5e94\u8be5\u4f9d\u7136\u7f6e\u4e8e\u8f93\u51fa\u53c2\u6570\u4e4b\u524d\u3002<\/p>\n<p>\u8fd9\u4e00\u70b9\u5e76\u4e0d\u662f\u5fc5\u987b\u9075\u5faa\u7684\u89c4\u5219\uff0c\u8f93\u5165\/\u8f93\u51fa\u4e24\u7528\u53c2\u6570\uff08\u901a\u5e38\u662f\u7c7b\/\u7ed3\u6784\u4f53\u53d8\u91cf\uff09\u6df7\u5728\u5176\u4e2d\uff0c\u4f1a\u4f7f\u5f97\u89c4\u5219\u96be\u4ee5\u9075\u5faa\u3002<\/p>\n<h2>6. \u5305\u542b\u6587\u4ef6\u7684\u540d\u79f0\u53ca\u6b21\u5e8f<\/h2>\n<p>\u5c06\u5305\u542b\u6b21\u5e8f\u6807\u51c6\u5316\u53ef\u589e\u5f3a\u53ef\u8bfb\u6027\u3001\u907f\u514d\u9690\u85cf\u4f9d\u8d56\uff08hidden dependencies\uff0c\u8bd1\u8005\u6ce8\uff1a\u9690\u85cf\u4f9d\u8d56\u4e3b\u8981\u662f\u6307\u5305\u542b\u7684\u6587\u4ef6\u4e2d\u7f16\u8bd1\u65f6\uff09\uff0c\u6b21\u5e8f\u5982\u4e0b\uff1aC\u5e93\u3001C++\u5e93\u3001\u5176\u4ed6\u5e93\u7684.h\u3001\u9879\u76ee\u5185\u7684.h\u3002<\/p>\n<p>\u9879\u76ee\u5185\u5934\u6587\u4ef6\u5e94\u6309\u7167\u9879\u76ee\u6e90\u4ee3\u7801\u76ee\u5f55\u6811\u7ed3\u6784\u6392\u5217\uff0c\u5e76\u4e14\u907f\u514d\u4f7f\u7528UNIX\u6587\u4ef6\u8def\u5f84.\uff08\u5f53\u524d\u76ee\u5f55\uff09\u548c..\uff08\u7236\u76ee\u5f55\uff09\u3002\u4f8b\u5982\uff0cgoogle-awesome-project\/src\/base\/logging.h\u5e94\u50cf\u8fd9\u6837\u88ab\u5305\u542b\uff1a<\/p>\n<p>#include &#8220;base\/logging.h&#8221;<\/p>\n<p>dir\/foo.cc\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u6267\u884c\u6216\u6d4b\u8bd5dir2\/foo2.h\u7684\u529f\u80fd\uff0cfoo.cc\u4e2d\u5305\u542b\u5934\u6587\u4ef6\u7684\u6b21\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p>dir2\/foo2.h\uff08\u4f18\u5148\u4f4d\u7f6e\uff0c\u8be6\u60c5\u5982\u4e0b\uff09<\/p>\n<p>C\u7cfb\u7edf\u6587\u4ef6<\/p>\n<p>C++\u7cfb\u7edf\u6587\u4ef6<\/p>\n<p>\u5176\u4ed6\u5e93\u5934\u6587\u4ef6<\/p>\n<p>\u672c\u9879\u76ee\u5185\u5934\u6587\u4ef6<\/p>\n<p>\u8fd9\u79cd\u6392\u5e8f\u65b9\u5f0f\u53ef\u6709\u6548\u51cf\u5c11\u9690\u85cf\u4f9d\u8d56\uff0c\u6211\u4eec\u5e0c\u671b\u6bcf\u4e00\u4e2a\u5934\u6587\u4ef6\u72ec\u7acb\u7f16\u8bd1\u3002\u6700\u7b80\u5355\u7684\u5b9e\u73b0\u65b9\u5f0f\u662f\u5c06\u5176\u4f5c\u4e3a\u7b2c\u4e00\u4e2a.h\u6587\u4ef6\u5305\u542b\u5728\u5bf9\u5e94\u7684.cc\u4e2d\u3002<\/p>\n<p>dir\/foo.cc\u548cdir2\/foo2.h\u901a\u5e38\u4f4d\u4e8e\u76f8\u540c\u76ee\u5f55\u4e0b\uff08\u50cfbase\/basictypes_unittest.cc\u548cbase\/basictypes.h\uff09\uff0c\u4f46\u4e5f\u53ef\u5728\u4e0d\u540c\u76ee\u5f55\u4e0b\u3002<\/p>\n<p>\u76f8\u540c\u76ee\u5f55\u4e0b\u5934\u6587\u4ef6\u6309\u5b57\u6bcd\u5e8f\u662f\u4e0d\u9519\u7684\u9009\u62e9\u3002<\/p>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff0cgoogle-awesome-project\/src\/foo\/internal\/fooserver.cc\u7684\u5305\u542b\u6b21\u5e8f\u5982\u4e0b\uff1a<\/p>\n<p>#include &#8220;foo\/public\/fooserver.h&#8221;\u00a0 \/\/ \u4f18\u5148\u4f4d\u7f6e<\/p>\n<p>#include &lt;sys\/types.h&gt;<\/p>\n<p>#include &lt;unistd.h&gt;<\/p>\n<p>#include &lt;hash_map&gt;<\/p>\n<p>#include &lt;vector&gt;<\/p>\n<p>#include &#8220;base\/basictypes.h&#8221;<\/p>\n<p>#include &#8220;base\/commandlineflags.h&#8221;<\/p>\n<p>#include &#8220;foo\/public\/bar.h&#8221;<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u82f1\u8bed\u4e0d\u592a\u597d\uff0c\u7ffb\u8bd1\u7684\u4e5f\u5c31\u4e0d\u592a\u597d\u3002\u8fd9\u4e00\u7bc7\u4e3b\u8981\u63d0\u5230\u7684\u662f\u5934\u6587\u4ef6\u7684\u4e00\u4e9b\u89c4\u5219\uff0c\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u907f\u514d\u591a\u91cd\u5305\u542b\u662f\u5b66\u7f16\u7a0b\u65f6\u6700\u57fa\u672c\u7684\u8981\u6c42\uff1b<\/strong><\/li>\n<li><strong>\u524d\u7f6e\u58f0\u660e\u662f\u4e3a\u4e86\u964d\u4f4e\u7f16\u8bd1\u4f9d\u8d56\uff0c\u9632\u6b62\u4fee\u6539\u4e00\u4e2a\u5934\u6587\u4ef6\u5f15\u53d1\u591a\u7c73\u8bfa\u6548\u5e94\uff1b<\/strong><\/li>\n<li><strong>\u5185\u8054\u51fd\u6570\u7684\u5408\u7406\u4f7f\u7528\u53ef\u63d0\u9ad8\u4ee3\u7801\u6267\u884c\u6548\u7387\uff1b<\/strong><\/li>\n<li><strong> -inl.h<\/strong><strong>\u53ef\u63d0\u9ad8\u4ee3\u7801\u53ef\u8bfb\u6027\uff08\u4e00\u822c\u7528\u4e0d\u5230\u5427<\/strong><strong>\ud83d\ude00<\/strong><strong>\uff09\uff1b<\/strong><\/li>\n<li><strong>\u6807\u51c6\u5316\u51fd\u6570\u53c2\u6570\u987a\u5e8f\u53ef\u4ee5\u63d0\u9ad8\u53ef\u8bfb\u6027\u548c\u6613\u7ef4\u62a4\u6027\uff08\u5bf9\u51fd\u6570\u53c2\u6570\u7684\u5806\u6808\u7a7a\u95f4\u6709\u8f7b\u5fae\u5f71\u54cd\uff0c\u6211\u4ee5\u524d\u5927\u591a\u662f\u76f8\u540c\u7c7b\u578b\u653e\u5728\u4e00\u8d77\uff09\uff1b<\/strong><\/li>\n<li><strong>\u5305\u542b\u6587\u4ef6\u7684\u540d\u79f0\u4f7f\u7528<\/strong><strong>.<\/strong><strong>\u548c<\/strong><strong>..<\/strong><strong>\u867d\u7136\u65b9\u4fbf\u5374\u6613\u6df7\u4e71\uff0c\u4f7f\u7528\u6bd4\u8f83\u5b8c\u6574\u7684\u9879\u76ee\u8def\u5f84\u770b\u4e0a\u53bb\u5f88\u6e05\u6670\u3001\u5f88\u6761\u7406\uff0c\u5305\u542b\u6587\u4ef6\u7684\u6b21\u5e8f\u9664\u4e86\u7f8e\u89c2\u4e4b\u5916\uff0c\u6700\u91cd\u8981\u7684\u662f\u53ef\u4ee5\u51cf\u5c11\u9690\u85cf\u4f9d\u8d56\uff0c\u4f7f\u6bcf\u4e2a\u5934\u6587\u4ef6\u5728<\/strong><strong>\u201c<\/strong><strong>\u6700\u9700\u8981\u7f16\u8bd1<\/strong><strong>\u201d<\/strong><strong>\uff08\u5bf9\u5e94\u6e90\u6587\u4ef6\u5904<\/strong><strong>\ud83d\ude00<\/strong><strong>\uff09\u7684\u5730\u65b9\u7f16\u8bd1\uff0c\u6709\u4eba\u63d0\u51fa\u5e93\u6587\u4ef6\u653e\u5728\u6700\u540e\uff0c\u8fd9\u6837\u51fa\u9519\u5148\u662f\u9879\u76ee\u5185\u7684\u6587\u4ef6\uff0c\u5934\u6587\u4ef6\u90fd\u653e\u5728\u5bf9\u5e94\u6e90\u6587\u4ef6\u7684\u6700\u524d\u9762\uff0c\u8fd9\u4e00\u70b9\u8db3\u4ee5\u4fdd\u8bc1\u5185\u90e8\u9519\u8bef\u7684\u53ca\u65f6\u53d1\u73b0\u4e86\u3002<\/strong><\/li>\n<\/ol>\n<h1>Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e8c\uff09<\/h1>\n<h2>\u4f5c\u7528\u57df<\/h2>\n<h2>1. \u547d\u540d\u7a7a\u95f4\uff08Namespaces\uff09<\/h2>\n<p>\u5728.cc\u6587\u4ef6\u4e2d\uff0c\u63d0\u5021\u4f7f\u7528\u4e0d\u5177\u540d\u7684\u547d\u540d\u7a7a\u95f4\uff08unnamed namespaces\uff0c\u8bd1\u8005\u6ce8\uff1a\u4e0d\u5177\u540d\u7684\u547d\u540d\u7a7a\u95f4\u5c31\u50cf\u4e0d\u5177\u540d\u7684\u7c7b\u4e00\u6837\uff0c\u4f3c\u4e4e\u88ab\u4ecb\u7ecd\u7684\u5f88\u5c11:-(\uff09\u3002\u4f7f\u7528\u5177\u540d\u547d\u540d\u7a7a\u95f4\u65f6\uff0c\u5176\u540d\u79f0\u53ef\u57fa\u4e8e\u9879\u76ee\u6216\u8def\u5f84\u540d\u79f0\uff0c\u4e0d\u8981\u4f7f\u7528using\u6307\u793a\u7b26\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u547d\u540d\u7a7a\u95f4\u5c06\u5168\u5c40\u4f5c\u7528\u57df\u7ec6\u5206\u4e3a\u4e0d\u540c\u7684\u3001\u5177\u540d\u7684\u4f5c\u7528\u57df\uff0c\u53ef\u6709\u6548\u9632\u6b62\u5168\u5c40\u4f5c\u7528\u57df\u7684\u547d\u540d\u51b2\u7a81\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u547d\u540d\u7a7a\u95f4\u63d0\u4f9b\u4e86\uff08\u53ef\u5d4c\u5957\uff09\u547d\u540d\u8f74\u7ebf\uff08name axis\uff0c\u8bd1\u8005\u6ce8\uff1a\u5c06\u547d\u540d\u5206\u5272\u5728\u4e0d\u540c\u547d\u540d\u7a7a\u95f4\u5185\uff09\uff0c\u5f53\u7136\uff0c\u7c7b\u4e5f\u63d0\u4f9b\u4e86\uff08\u53ef\u5d4c\u5957\uff09\u7684\u547d\u540d\u8f74\u7ebf\uff08\u8bd1\u8005\u6ce8\uff1a\u5c06\u547d\u540d\u5206\u5272\u5728\u4e0d\u540c\u7c7b\u7684\u4f5c\u7528\u57df\u5185\uff09\u3002<\/p>\n<p>\u4e3e\u4f8b\u6765\u8bf4\uff0c\u4e24\u4e2a\u4e0d\u540c\u9879\u76ee\u7684\u5168\u5c40\u4f5c\u7528\u57df\u90fd\u6709\u4e00\u4e2a\u7c7bFoo\uff0c\u8fd9\u6837\u5728\u7f16\u8bd1\u6216\u8fd0\u884c\u65f6\u9020\u6210\u51b2\u7a81\u3002\u5982\u679c\u6bcf\u4e2a\u9879\u76ee\u5c06\u4ee3\u7801\u7f6e\u4e8e\u4e0d\u540c\u547d\u540d\u7a7a\u95f4\u4e2d\uff0cproject1::Foo\u548cproject2::Foo\u4f5c\u4e3a\u4e0d\u540c\u7b26\u53f7\u81ea\u7136\u4e0d\u4f1a\u51b2\u7a81\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u547d\u540d\u7a7a\u95f4\u5177\u6709\u8ff7\u60d1\u6027\uff0c\u56e0\u4e3a\u5b83\u4eec\u548c\u7c7b\u4e00\u6837\u63d0\u4f9b\u4e86\u989d\u5916\u7684\uff08\u53ef\u5d4c\u5957\u7684\uff09\u547d\u540d\u8f74\u7ebf\u3002\u5728\u5934\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e0d\u5177\u540d\u7684\u7a7a\u95f4\u5bb9\u6613\u8fdd\u80ccC++\u7684\u552f\u4e00\u5b9a\u4e49\u539f\u5219\uff08One Definition Rule (ODR)\uff09\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u6839\u636e\u4e0b\u6587\u5c06\u8981\u63d0\u5230\u7684\u7b56\u7565\u5408\u7406\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<h3>1) \u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\uff08Unnamed Namespaces\uff09<\/h3>\n<p>\u5728.cc\u6587\u4ef6\u4e2d\uff0c\u5141\u8bb8\u751a\u81f3\u63d0\u5021\u4f7f\u7528\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\uff0c\u4ee5\u907f\u514d\u8fd0\u884c\u65f6\u7684\u547d\u540d\u51b2\u7a81\uff1a<\/p>\n<p>namespace {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ .cc \u6587\u4ef6\u4e2d<\/p>\n<p>\/\/ \u547d\u540d\u7a7a\u95f4\u7684\u5185\u5bb9\u65e0\u9700\u7f29\u8fdb<\/p>\n<p>enum { UNUSED, EOF, ERROR };\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u7ecf\u5e38\u4f7f\u7528\u7684\u7b26\u53f7<\/p>\n<p>bool AtEof() { return pos_ == EOF; }\u00a0\u00a0 \/\/ \u4f7f\u7528\u672c\u547d\u540d\u7a7a\u95f4\u5185\u7684\u7b26\u53f7EOF<\/p>\n<p>}\u00a0 \/\/ namespace<\/p>\n<p>\u7136\u800c\uff0c\u4e0e\u7279\u5b9a\u7c7b\u5173\u8054\u7684\u6587\u4ef6\u4f5c\u7528\u57df\u58f0\u660e\u5728\u8be5\u7c7b\u4e2d\u88ab\u58f0\u660e\u4e3a\u7c7b\u578b\u3001\u9759\u6001\u6570\u636e\u6210\u5458\u6216\u9759\u6001\u6210\u5458\u51fd\u6570\uff0c\u800c\u4e0d\u662f\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u7684\u6210\u5458\u3002\u50cf\u4e0a\u6587\u5c55\u793a\u7684\u90a3\u6837\uff0c\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u7ed3\u675f\u65f6\u7528\u6ce8\u91ca\/\/ namespace\u6807\u8bc6\u3002<\/p>\n<p>\u4e0d\u80fd\u5728.h\u6587\u4ef6\u4e2d\u4f7f\u7528\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<h3>2) \u5177\u540d\u547d\u540d\u7a7a\u95f4\uff08Named Namespaces\uff09<\/h3>\n<p>\u5177\u540d\u547d\u540d\u7a7a\u95f4\u4f7f\u7528\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>\u547d\u540d\u7a7a\u95f4\u5c06\u9664\u6587\u4ef6\u5305\u542b\u3001\u5168\u5c40\u6807\u8bc6\u7684\u58f0\u660e\/\u5b9a\u4e49\u4ee5\u53ca\u7c7b\u7684\u524d\u7f6e\u58f0\u660e\u5916\u7684\u6574\u4e2a\u6e90\u6587\u4ef6\u5c01\u88c5\u8d77\u6765\uff0c\u4ee5\u540c\u5176\u4ed6\u547d\u540d\u7a7a\u95f4\u76f8\u533a\u5206\u3002<\/p>\n<p>\/\/ .h\u6587\u4ef6<\/p>\n<p>namespace mynamespace {<\/p>\n<p>\/\/ \u6240\u6709\u58f0\u660e\u90fd\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u4e2d<\/p>\n<p>\/\/ \u6ce8\u610f\u4e0d\u8981\u4f7f\u7528\u7f29\u8fdb<\/p>\n<p>class MyClass {<\/p>\n<p>public:<\/p>\n<p>&#8230;<\/p>\n<p>void Foo();<\/p>\n<p>};<\/p>\n<p>}\u00a0 \/\/ namespace mynamespace<\/p>\n<p>\/\/ .cc\u6587\u4ef6<\/p>\n<p>namespace mynamespace {<\/p>\n<p>\/\/ \u51fd\u6570\u5b9a\u4e49\u90fd\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u4e2d<\/p>\n<p>void MyClass::Foo() {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>}\u00a0 \/\/ namespace mynamespace<\/p>\n<p>\u901a\u5e38\u7684.cc\u6587\u4ef6\u4f1a\u5305\u542b\u66f4\u591a\u3001\u66f4\u590d\u6742\u7684\u7ec6\u8282\uff0c\u5305\u62ec\u5bf9\u5176\u4ed6\u547d\u540d\u7a7a\u95f4\u4e2d\u7c7b\u7684\u5f15\u7528\u7b49\u3002<\/p>\n<p>#include &#8220;a.h&#8221;<\/p>\n<p>DEFINE_bool(someflag, false, &#8220;dummy flag&#8221;);<\/p>\n<p>class C;\u00a0 \/\/ \u5168\u5c40\u547d\u540d\u7a7a\u95f4\u4e2d\u7c7bC\u7684\u524d\u7f6e\u58f0\u660e<\/p>\n<p>namespace a { class A; }\u00a0 \/\/ \u547d\u540d\u7a7a\u95f4a\u4e2d\u7684\u7c7ba::A\u7684\u524d\u7f6e\u58f0\u660e<\/p>\n<p>namespace b {<\/p>\n<p>&#8230;code for b&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ b\u4e2d\u7684\u4ee3\u7801<\/p>\n<p>}\u00a0 \/\/ namespace b<\/p>\n<p>\u4e0d\u8981\u58f0\u660e\u547d\u540d\u7a7a\u95f4std\u4e0b\u7684\u4efb\u4f55\u5185\u5bb9\uff0c\u5305\u62ec\u6807\u51c6\u5e93\u7c7b\u7684\u524d\u7f6e\u58f0\u660e\u3002\u58f0\u660estd\u4e0b\u7684\u5b9e\u4f53\u4f1a\u5bfc\u81f4\u4e0d\u660e\u786e\u7684\u884c\u4e3a\uff0c\u5982\uff0c\u4e0d\u53ef\u79fb\u690d\u3002\u58f0\u660e\u6807\u51c6\u5e93\u4e0b\u7684\u5b9e\u4f53\uff0c\u9700\u8981\u5305\u542b\u5bf9\u5e94\u7684\u5934\u6587\u4ef6\u3002<\/p>\n<p>\u6700\u597d\u4e0d\u8981\u4f7f\u7528using\u6307\u793a\u7b26\uff0c\u4ee5\u4fdd\u8bc1\u547d\u540d\u7a7a\u95f4\u4e0b\u7684\u6240\u6709\u540d\u79f0\u90fd\u53ef\u4ee5\u6b63\u5e38\u4f7f\u7528\u3002<\/p>\n<p>\/\/ \u7981\u6b62\u2014\u2014\u6c61\u67d3\u547d\u540d\u7a7a\u95f4<\/p>\n<p>using namespace foo;<\/p>\n<p>\u5728.cc\u6587\u4ef6\u3001.h\u6587\u4ef6\u7684\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u7c7b\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528using\u3002<\/p>\n<p>\/\/ \u5141\u8bb8\uff1a.cc\u6587\u4ef6\u4e2d<\/p>\n<p>\/\/ .h\u6587\u4ef6\u4e2d\uff0c\u5fc5\u987b\u5728\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u7c7b\u7684\u5185\u90e8\u4f7f\u7528<\/p>\n<p>using ::foo::bar;<\/p>\n<p>\u5728.cc\u6587\u4ef6\u3001.h\u6587\u4ef6\u7684\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u7c7b\u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u522b\u540d\u3002<\/p>\n<p>\/\/ \u5141\u8bb8\uff1a.cc\u6587\u4ef6\u4e2d<\/p>\n<p>\/\/ .h\u6587\u4ef6\u4e2d\uff0c\u5fc5\u987b\u5728\u51fd\u6570\u3001\u65b9\u6cd5\u6216\u7c7b\u7684\u5185\u90e8\u4f7f\u7528<\/p>\n<p>namespace fbz = ::foo::bar::baz;<\/p>\n<h2>2. \u5d4c\u5957\u7c7b\uff08Nested Class\uff09<\/h2>\n<p>\u5f53\u516c\u5f00\u5d4c\u5957\u7c7b\u4f5c\u4e3a\u63a5\u53e3\u7684\u4e00\u90e8\u5206\u65f6\uff0c\u867d\u7136\u53ef\u4ee5\u76f4\u63a5\u5c06\u4ed6\u4eec\u4fdd\u6301\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\uff0c\u4f46\u5c06\u5d4c\u5957\u7c7b\u7684\u58f0\u660e\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u4e2d\u662f\u66f4\u597d\u7684\u9009\u62e9\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u53ef\u4ee5\u5728\u4e00\u4e2a\u7c7b\u4e2d\u5b9a\u4e49\u53e6\u4e00\u4e2a\u7c7b\uff0c\u5d4c\u5957\u7c7b\u4e5f\u79f0\u6210\u5458\u7c7b\uff08member class\uff09\u3002<\/p>\n<p>class Foo {<\/p>\n<p>private:<\/p>\n<p>\/\/ Bar\u662f\u5d4c\u5957\u5728Foo\u4e2d\u7684\u6210\u5458\u7c7b<\/p>\n<p>class Bar {<\/p>\n<p>&#8230;<\/p>\n<p>};<\/p>\n<p>};<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u5f53\u5d4c\u5957\uff08\u6210\u5458\uff09\u7c7b\u53ea\u5728\u88ab\u5d4c\u5957\u7c7b\uff08enclosing class\uff09\u4e2d\u4f7f\u7528\u65f6\u5f88\u6709\u7528\uff0c\u5c06\u5176\u7f6e\u4e8e\u88ab\u5d4c\u5957\u7c7b\u4f5c\u7528\u57df\u4f5c\u4e3a\u88ab\u5d4c\u5957\u7c7b\u7684\u6210\u5458\u4e0d\u4f1a\u6c61\u67d3\u5176\u4ed6\u4f5c\u7528\u57df\u540c\u540d\u7c7b\u3002\u53ef\u5728\u88ab\u5d4c\u5957\u7c7b\u4e2d\u524d\u7f6e\u58f0\u660e\u5d4c\u5957\u7c7b\uff0c\u5728.cc\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5d4c\u5957\u7c7b\uff0c\u907f\u514d\u5728\u88ab\u5d4c\u5957\u7c7b\u4e2d\u5305\u542b\u5d4c\u5957\u7c7b\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u5d4c\u5957\u7c7b\u7684\u5b9a\u4e49\u901a\u5e38\u53ea\u4e0e\u5b9e\u73b0\u76f8\u5173\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u53ea\u80fd\u5728\u88ab\u5d4c\u5957\u7c7b\u7684\u5b9a\u4e49\u4e2d\u624d\u80fd\u524d\u7f6e\u58f0\u660e\u5d4c\u5957\u7c7b\u3002\u56e0\u6b64\uff0c\u4efb\u4f55\u4f7f\u7528Foo::Bar*\u6307\u9488\u7684\u5934\u6587\u4ef6\u5fc5\u987b\u5305\u542b\u6574\u4e2aFoo\u7684\u58f0\u660e\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u4e0d\u8981\u5c06\u5d4c\u5957\u7c7b\u5b9a\u4e49\u4e3apublic\uff0c\u9664\u975e\u5b83\u4eec\u662f\u63a5\u53e3\u7684\u4e00\u90e8\u5206\uff0c\u6bd4\u5982\uff0c\u67d0\u4e2a\u65b9\u6cd5\u4f7f\u7528\u4e86\u8fd9\u4e2a\u7c7b\u7684\u4e00\u7cfb\u5217\u9009\u9879\u3002<\/p>\n<h2>3. \u975e\u6210\u5458\u51fd\u6570\uff08Nonmember\uff09\u3001\u9759\u6001\u6210\u5458\u51fd\u6570\uff08Static Member\uff09\u548c\u5168\u5c40\u51fd\u6570\uff08Global Functions\uff09<\/h2>\n<p>\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u4e2d\u7684\u975e\u6210\u5458\u51fd\u6570\u6216\u9759\u6001\u6210\u5458\u51fd\u6570\uff0c\u5c3d\u91cf\u4e0d\u8981\u4f7f\u7528\u5168\u5c40\u51fd\u6570\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u975e\u6210\u5458\u51fd\u6570\u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u662f\u975e\u5e38\u6709\u7528\u7684\uff0c\u5c06\u975e\u6210\u5458\u51fd\u6570\u7f6e\u4e8e\u547d\u540d\u7a7a\u95f4\u4e2d\u53ef\u907f\u514d\u5bf9\u5168\u5c40\u4f5c\u7528\u57df\u7684\u6c61\u67d3\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5c06\u975e\u6210\u5458\u51fd\u6570\u548c\u9759\u6001\u6210\u5458\u51fd\u6570\u4f5c\u4e3a\u65b0\u7c7b\u7684\u6210\u5458\u6216\u8bb8\u66f4\u6709\u610f\u4e49\uff0c\u5f53\u5b83\u4eec\u9700\u8981\u8bbf\u95ee\u5916\u90e8\u8d44\u6e90\u6216\u5177\u6709\u91cd\u8981\u4f9d\u8d56\u65f6\u66f4\u662f\u5982\u6b64\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u6709\u65f6\uff0c\u4e0d\u628a\u51fd\u6570\u9650\u5b9a\u5728\u7c7b\u7684\u5b9e\u4f53\u4e2d\u662f\u6709\u76ca\u7684\uff0c\u751a\u81f3\u9700\u8981\u8fd9\u4e48\u505a\uff0c\u8981\u4e48\u4f5c\u4e3a\u9759\u6001\u6210\u5458\uff0c\u8981\u4e48\u4f5c\u4e3a\u975e\u6210\u5458\u51fd\u6570\u3002\u975e\u6210\u5458\u51fd\u6570\u4e0d\u5e94\u4f9d\u8d56\u4e8e\u5916\u90e8\u53d8\u91cf\uff0c\u5e76\u5c3d\u91cf\u7f6e\u4e8e\u67d0\u4e2a\u547d\u540d\u7a7a\u95f4\u4e2d\u3002\u76f8\u6bd4\u5355\u7eaf\u4e3a\u4e86\u5c01\u88c5\u82e5\u5e72\u4e0d\u5171\u4eab\u4efb\u4f55\u9759\u6001\u6570\u636e\u7684\u9759\u6001\u6210\u5458\u51fd\u6570\u800c\u521b\u5efa\u7c7b\uff0c\u4e0d\u5982\u4f7f\u7528\u547d\u540d\u7a7a\u95f4\u3002<\/p>\n<p>\u5b9a\u4e49\u4e8e\u540c\u4e00\u7f16\u8bd1\u5355\u5143\u7684\u51fd\u6570\uff0c\u88ab\u5176\u4ed6\u7f16\u8bd1\u5355\u5143\u76f4\u63a5\u8c03\u7528\u53ef\u80fd\u4f1a\u5f15\u5165\u4e0d\u5fc5\u8981\u7684\u8026\u5408\u548c\u8fde\u63a5\u4f9d\u8d56\uff1b\u9759\u6001\u6210\u5458\u51fd\u6570\u5bf9\u6b64\u5c24\u5176\u654f\u611f\u3002\u53ef\u4ee5\u8003\u8651\u63d0\u53d6\u5230\u65b0\u7c7b\u4e2d\uff0c\u6216\u8005\u5c06\u51fd\u6570\u7f6e\u4e8e\u72ec\u7acb\u5e93\u7684\u547d\u540d\u7a7a\u95f4\u4e2d\u3002<\/p>\n<p>\u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u5b9a\u4e49\u975e\u6210\u5458\u51fd\u6570\uff0c\u53c8\u53ea\u662f\u5728.cc\u6587\u4ef6\u4e2d\u4f7f\u7528\u5b83\uff0c\u53ef\u4f7f\u7528\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u6216static\u5173\u8054\uff08\u5982static int Foo() {&#8230;}\uff09\u9650\u5b9a\u5176\u4f5c\u7528\u57df\u3002<\/p>\n<h2>4. \u5c40\u90e8\u53d8\u91cf\uff08Local Variables\uff09<\/h2>\n<p>\u5c06\u51fd\u6570\u53d8\u91cf\u5c3d\u53ef\u80fd\u7f6e\u4e8e\u6700\u5c0f\u4f5c\u7528\u57df\u5185\uff0c\u5728\u58f0\u660e\u53d8\u91cf\u65f6\u5c06\u5176\u521d\u59cb\u5316\u3002<\/p>\n<p>C++\u5141\u8bb8\u5728\u51fd\u6570\u7684\u4efb\u4f55\u4f4d\u7f6e\u58f0\u660e\u53d8\u91cf\u3002\u6211\u4eec\u63d0\u5021\u5728\u5c3d\u53ef\u80fd\u5c0f\u7684\u4f5c\u7528\u57df\u4e2d\u58f0\u660e\u53d8\u91cf\uff0c\u79bb\u7b2c\u4e00\u6b21\u4f7f\u7528\u8d8a\u8fd1\u8d8a\u597d\u3002\u8fd9\u4f7f\u5f97\u4ee3\u7801\u6613\u4e8e\u9605\u8bfb\uff0c\u6613\u4e8e\u5b9a\u4f4d\u53d8\u91cf\u7684\u58f0\u660e\u4f4d\u7f6e\u3001\u53d8\u91cf\u7c7b\u578b\u548c\u521d\u59cb\u503c\u3002\u7279\u522b\u662f\uff0c\u5e94\u4f7f\u7528\u521d\u59cb\u5316\u4ee3\u66ff\u58f0\u660e+\u8d4b\u503c\u7684\u65b9\u5f0f\u3002<\/p>\n<p>int i;<\/p>\n<p>i = f();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ \u574f\u2014\u2014\u521d\u59cb\u5316\u548c\u58f0\u660e\u5206\u79bb<\/p>\n<p>nt j = g();\u00a0\u00a0 \/\/ \u597d\u2014\u2014\u521d\u59cb\u5316\u65f6\u58f0\u660e<\/p>\n<p>\u6ce8\u610f\uff1agcc\u53ef\u6b63\u786e\u6267\u884cfor (int i = 0; i &lt; 10; ++i)\uff08i\u7684\u4f5c\u7528\u57df\u4ec5\u9650for\u5faa\u73af\uff09\uff0c\u56e0\u6b64\u5176\u4ed6for\u5faa\u73af\u4e2d\u53ef\u91cd\u7528i\u3002if\u548cwhile\u7b49\u8bed\u53e5\u4e2d\uff0c\u4f5c\u7528\u57df\u58f0\u660e\uff08scope declaration\uff09\u540c\u6837\u662f\u6b63\u786e\u7684\u3002<\/p>\n<p>while (const char* p = strchr(str, &#8216;\/&#8217;)) str = p + 1;<\/p>\n<p>\u6ce8\u610f\uff1a\u5982\u679c\u53d8\u91cf\u662f\u4e00\u4e2a\u5bf9\u8c61\uff0c\u6bcf\u6b21\u8fdb\u5165\u4f5c\u7528\u57df\u90fd\u8981\u8c03\u7528\u5176\u6784\u9020\u51fd\u6570\uff0c\u6bcf\u6b21\u9000\u51fa\u4f5c\u7528\u57df\u90fd\u8981\u8c03\u7528\u5176\u6790\u6784\u51fd\u6570\u3002<\/p>\n<p>\/\/ \u4f4e\u6548\u7684\u5b9e\u73b0<\/p>\n<p>for (int i = 0; i &lt; 1000000; ++i) {<\/p>\n<p>Foo f;\u00a0 \/\/ \u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u5206\u522b\u8c03\u75281000000\u6b21\uff01<\/p>\n<p>f.DoSomething(i);<\/p>\n<p>}<\/p>\n<p>\u7c7b\u4f3c\u53d8\u91cf\u653e\u5230\u5faa\u73af\u4f5c\u7528\u57df\u5916\u9762\u58f0\u660e\u8981\u9ad8\u6548\u7684\u591a\uff1a<\/p>\n<p>Foo f;\u00a0 \/\/ \u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570\u53ea\u8c03\u75281\u6b21<\/p>\n<p>for (int i = 0; i &lt; 1000000; ++i) {<\/p>\n<p>f.DoSomething(i);<\/p>\n<p>}<\/p>\n<h2>5. \u5168\u5c40\u53d8\u91cf\uff08Global Variables\uff09<\/h2>\n<p>class\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u88ab\u7981\u6b62\u7684\uff0c\u5185\u5efa\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\u662f\u5141\u8bb8\u7684\uff0c\u5f53\u7136\u591a\u7ebf\u7a0b\u4ee3\u7801\u4e2d\u975e\u5e38\u6570\u5168\u5c40\u53d8\u91cf\u4e5f\u662f\u88ab\u7981\u6b62\u7684\u3002\u6c38\u8fdc\u4e0d\u8981\u4f7f\u7528\u51fd\u6570\u8fd4\u56de\u503c\u521d\u59cb\u5316\u5168\u5c40\u53d8\u91cf\u3002<\/p>\n<p>\u4e0d\u5e78\u7684\u662f\uff0c\u5168\u5c40\u53d8\u91cf\u7684\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u4ee5\u53ca\u521d\u59cb\u5316\u64cd\u4f5c\u7684\u8c03\u7528\u987a\u5e8f\u53ea\u662f\u88ab\u90e8\u5206\u89c4\u5b9a\uff0c\u6bcf\u6b21\u751f\u6210\u6709\u53ef\u80fd\u4f1a\u6709\u53d8\u5316\uff0c\u4ece\u800c\u5bfc\u81f4\u96be\u4ee5\u53d1\u73b0\u7684bugs\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u7981\u6b62\u4f7f\u7528class\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u5305\u62ecSTL\u7684string, vector\u7b49\u7b49\uff09\uff0c\u56e0\u4e3a\u5b83\u4eec\u7684\u521d\u59cb\u5316\u987a\u5e8f\u6709\u53ef\u80fd\u5bfc\u81f4\u6784\u9020\u51fa\u73b0\u95ee\u9898\u3002\u5185\u5efa\u7c7b\u578b\u548c\u7531\u5185\u5efa\u7c7b\u578b\u6784\u6210\u7684\u6ca1\u6709\u6784\u9020\u51fd\u6570\u7684\u7ed3\u6784\u4f53\u53ef\u4ee5\u4f7f\u7528\uff0c\u5982\u679c\u4f60\u4e00\u5b9a\u8981\u4f7f\u7528class\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff0c\u8bf7\u4f7f\u7528\u5355\u4ef6\u6a21\u5f0f\uff08singleton pattern\uff09\u3002<\/p>\n<p>\u5bf9\u4e8e\u5168\u5c40\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u4f7f\u7528C\u98ce\u683c\u7684\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u8981\u4f7f\u7528STL\u7684\u5b57\u7b26\u4e32\uff1a<\/p>\n<p>const char kFrogSays[] = &#8220;ribbet&#8221;;<\/p>\n<p>\u867d\u7136\u5141\u8bb8\u5728\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u4f7f\u7528\u5168\u5c40\u53d8\u91cf\uff0c\u4f7f\u7528\u65f6\u52a1\u5fc5\u4e09\u601d\u3002\u5927\u591a\u6570\u5168\u5c40\u53d8\u91cf\u5e94\u8be5\u662f\u7c7b\u7684\u9759\u6001\u6570\u636e\u6210\u5458\uff0c\u6216\u8005\u5f53\u5176\u53ea\u5728.cc\u6587\u4ef6\u4e2d\u4f7f\u7528\u65f6\uff0c\u5c06\u5176\u5b9a\u4e49\u5230\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u4e2d\uff0c\u6216\u8005\u4f7f\u7528\u9759\u6001\u5173\u8054\u4ee5\u9650\u5236\u53d8\u91cf\u7684\u4f5c\u7528\u57df\u3002<\/p>\n<p>\u8bb0\u4f4f\uff0c\u9759\u6001\u6210\u5458\u53d8\u91cf\u89c6\u4f5c\u5168\u5c40\u53d8\u91cf\uff0c\u6240\u4ee5\uff0c\u4e5f\u4e0d\u80fd\u662fclass\u7c7b\u578b\uff01<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u8fd9\u4e00\u7bc7\u4e3b\u8981\u63d0\u5230\u7684\u662f\u4f5c\u7528\u57df\u7684\u4e00\u4e9b\u89c4\u5219\uff0c\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li><strong> .cc<\/strong><strong>\u4e2d\u7684\u4e0d\u5177\u540d\u547d\u540d\u7a7a\u95f4\u53ef\u907f\u514d\u547d\u540d\u51b2\u7a81\u3001\u9650\u5b9a\u4f5c\u7528\u57df\uff0c\u907f\u514d\u76f4\u63a5\u4f7f\u7528<\/strong><strong>using<\/strong><strong>\u63d0\u793a\u7b26\u6c61\u67d3\u547d\u540d\u7a7a\u95f4\uff1b<\/strong><\/li>\n<li><strong>\u5d4c\u5957\u7c7b\u7b26\u5408\u5c40\u90e8\u4f7f\u7528\u539f\u5219\uff0c\u53ea\u662f\u4e0d\u80fd\u5728\u5176\u4ed6\u5934\u6587\u4ef6\u4e2d\u524d\u7f6e\u58f0\u660e\uff0c\u5c3d\u91cf\u4e0d\u8981<\/strong><strong>public<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u5c3d\u91cf\u4e0d\u7528\u5168\u5c40\u51fd\u6570\u548c\u5168\u5c40\u53d8\u91cf\uff0c\u8003\u8651\u4f5c\u7528\u57df\u548c\u547d\u540d\u7a7a\u95f4\u9650\u5236\uff0c\u5c3d\u91cf\u5355\u72ec\u5f62\u6210\u7f16\u8bd1\u5355\u5143\uff1b<\/strong><\/li>\n<li><strong>\u591a\u7ebf\u7a0b\u4e2d\u7684\u5168\u5c40\u53d8\u91cf\uff08\u542b\u9759\u6001\u6210\u5458\u53d8\u91cf\uff09\u4e0d\u8981\u4f7f\u7528<\/strong><strong>class<\/strong><strong>\u7c7b\u578b\uff08\u542b<\/strong><strong>STL<\/strong><strong>\u5bb9\u5668\uff09\uff0c\u907f\u514d\u4e0d\u660e\u786e\u884c\u4e3a\u5bfc\u81f4\u7684<\/strong><strong>bugs<\/strong><strong>\u3002<\/strong><\/li>\n<\/ol>\n<p><strong>\u4f5c\u7528\u57df\u7684\u4f7f\u7528\uff0c\u9664\u4e86\u8003\u8651\u540d\u79f0\u6c61\u67d3\u3001\u53ef\u8bfb\u6027\u4e4b\u5916\uff0c\u4e3b\u8981\u662f\u4e3a\u964d\u4f4e\u8026\u5408\u5ea6\uff0c\u63d0\u9ad8\u7f16\u8bd1\u3001\u6267\u884c\u6548\u7387\u3002<\/strong><\/p>\n<h1>Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e09\uff09<\/h1>\n<p>\u8fd9\u4e00\u7bc7\u4e3b\u8981\u63d0\u5230\u7684\u662f\u7c7b\uff0cLippman\u5728\u300a<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/16\/www.amazon.com\/Inside-Object-Model-Stanley-Lippman\/dp\/0201834545\">Inside The C++ Object Model<\/a>\u300b\u7b2c\u4e8c\u7ae0\u4e2d\u5bf9\u6784\u9020\u51fd\u6570\u4f5c\u4e86\u8be6\u5c3d\u8bf4\u660e\uff0c\u672c\u6587\u4e2d\u63d0\u5230\u7684\u51e0\u4e2a\u5355\u8bcd\u57fa\u672c\u4eff\u8be5\u4e66\u4e2d\u8bd1\u672c\u4faf\u6377\u5148\u751f\u7684\u7ffb\u8bd1\uff1a<\/p>\n<p>explicit\uff1a\u660e\u786e\u7684<\/p>\n<p>implicit\uff1a\u9690\u542b\u7684<\/p>\n<p>trivial\uff1a\u6ca1\u6709\u610f\u4e49\u7684<\/p>\n<p>non-trivial\uff1a\u6709\u610f\u4e49\u7684<\/p>\n<p>&nbsp;<\/p>\n<p>\u539f\u6587\u5730\u5740\uff1a<a href=\"http:\/\/google-styleguide.googlecode.com\/svn\/trunk\/cppguide.xml?showone=Displaying_Hidden_Details_in_this_Guide\">Google C++ Style Guide<\/a><\/p>\n<h2>\u7c7b<\/h2>\n<p>\u7c7b\u662fC++\u4e2d\u57fa\u672c\u7684\u4ee3\u7801\u5355\u5143\uff0c\u81ea\u7136\u88ab\u5e7f\u6cdb\u4f7f\u7528\u3002\u672c\u8282\u5217\u4e3e\u4e86\u5728\u5199\u4e00\u4e2a\u7c7b\u65f6\u8981\u505a\u4ec0\u4e48\u3001\u4e0d\u8981\u505a\u4ec0\u4e48\u3002<\/p>\n<h2>1. \u6784\u9020\u51fd\u6570\uff08Constructor\uff09\u7684\u804c\u8d23<\/h2>\n<p>\u6784\u9020\u51fd\u6570\u4e2d\u53ea\u8fdb\u884c\u90a3\u4e9b\u6ca1\u6709\u5b9e\u9645\u610f\u4e49\u7684\uff08trivial\uff0c\u8bd1\u8005\u6ce8\uff1a\u7b80\u5355\u521d\u59cb\u5316\u5bf9\u4e8e\u7a0b\u5e8f\u6267\u884c\u6ca1\u6709\u5b9e\u9645\u7684\u903b\u8f91\u610f\u4e49\uff0c\u56e0\u4e3a\u6210\u5458\u53d8\u91cf\u7684\u201c\u6709\u610f\u4e49\u201d\u7684\u503c\u5927\u591a\u4e0d\u5728\u6784\u9020\u51fd\u6570\u4e2d\u786e\u5b9a\uff09\u521d\u59cb\u5316\uff0c\u53ef\u80fd\u7684\u8bdd\uff0c\u4f7f\u7528Init()\u65b9\u6cd5\u96c6\u4e2d\u521d\u59cb\u5316\u4e3a\u6709\u610f\u4e49\u7684\uff08non-trivial\uff09\u6570\u636e\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u6267\u884c\u521d\u59cb\u5316\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u6392\u7248\u65b9\u4fbf\uff0c\u65e0\u9700\u62c5\u5fc3\u7c7b\u662f\u5426\u521d\u59cb\u5316\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5728\u6784\u9020\u51fd\u6570\u4e2d\u6267\u884c\u64cd\u4f5c\u5f15\u8d77\u7684\u95ee\u9898\u6709\uff1a<\/p>\n<p>1) \u6784\u9020\u51fd\u6570\u4e2d\u4e0d\u6613\u62a5\u544a\u9519\u8bef\uff0c\u4e0d\u80fd\u4f7f\u7528\u5f02\u5e38\u3002<\/p>\n<p>2) \u64cd\u4f5c\u5931\u8d25\u4f1a\u9020\u6210\u5bf9\u8c61\u521d\u59cb\u5316\u5931\u8d25\uff0c\u5f15\u8d77\u4e0d\u786e\u5b9a\u72b6\u6001\u3002<\/p>\n<p>3) \u6784\u9020\u51fd\u6570\u5185\u8c03\u7528\u865a\u51fd\u6570\uff0c\u8c03\u7528\u4e0d\u4f1a\u6d3e\u53d1\u5230\u5b50\u7c7b\u5b9e\u73b0\u4e2d\uff0c\u5373\u4f7f\u5f53\u524d\u6ca1\u6709\u5b50\u7c7b\u5316\u5b9e\u73b0\uff0c\u5c06\u6765\u4ecd\u662f\u9690\u60a3\u3002<\/p>\n<p>4) \u5982\u679c\u6709\u4eba\u521b\u5efa\u8be5\u7c7b\u578b\u7684\u5168\u5c40\u53d8\u91cf\uff08\u867d\u7136\u8fdd\u80cc\u4e86\u4e0a\u8282\u63d0\u5230\u7684\u89c4\u5219\uff09\uff0c\u6784\u9020\u51fd\u6570\u5c06\u5728main()\u4e4b\u524d\u88ab\u8c03\u7528\uff0c\u6709\u53ef\u80fd\u7834\u574f\u6784\u9020\u51fd\u6570\u4e2d\u6697\u542b\u7684\u5047\u8bbe\u6761\u4ef6\u3002\u4f8b\u5982\uff0cgflags\u5c1a\u672a\u521d\u59cb\u5316\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u5982\u679c\u5bf9\u8c61\u9700\u8981\u6709\u610f\u4e49\u7684\uff08non-trivial\uff09\u521d\u59cb\u5316\uff0c\u8003\u8651\u4f7f\u7528\u53e6\u5916\u7684Init()\u65b9\u6cd5\u5e76\uff08\u6216\uff09\u589e\u52a0\u4e00\u4e2a\u6210\u5458\u6807\u8bb0\u7528\u4e8e\u6307\u793a\u5bf9\u8c61\u662f\u5426\u5df2\u7ecf\u521d\u59cb\u5316\u6210\u529f\u3002<\/p>\n<h2>2. \u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08Default Constructors\uff09<\/h2>\n<p>\u5982\u679c\u4e00\u4e2a\u7c7b\u5b9a\u4e49\u4e86\u82e5\u5e72\u6210\u5458\u53d8\u91cf\u53c8\u6ca1\u6709\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5426\u5219\u7f16\u8bd1\u5668\u5c06\u81ea\u52a8\u751f\u4ea7\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u65b0\u5efa\u4e00\u4e2a\u6ca1\u6709\u53c2\u6570\u7684\u5bf9\u8c61\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u5f53\u8c03\u7528new[]\uff08\u4e3a\u6570\u7ec4\uff09\u65f6\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u603b\u662f\u88ab\u8c03\u7528\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u9ed8\u8ba4\u5c06\u7ed3\u6784\u4f53\u521d\u59cb\u5316\u4e3a\u201c\u4e0d\u53ef\u80fd\u7684\u201d\u503c\uff0c\u4f7f\u8c03\u8bd5\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5bf9\u4ee3\u7801\u7f16\u5199\u8005\u6765\u8bf4\uff0c\u8fd9\u662f\u591a\u4f59\u7684\u5de5\u4f5c\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u5982\u679c\u7c7b\u4e2d\u5b9a\u4e49\u4e86\u6210\u5458\u53d8\u91cf\uff0c\u6ca1\u6709\u63d0\u4f9b\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff08\u6ca1\u6709\u53c2\u6570\uff09\u3002\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u66f4\u9002\u5408\u4e8e\u521d\u59cb\u5316\u5bf9\u8c61\uff0c\u4f7f\u5bf9\u8c61\u5185\u90e8\u72b6\u6001\uff08internal state\uff09\u4e00\u81f4\u3001\u6709\u6548\u3002<\/p>\n<p>\u63d0\u4f9b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7684\u539f\u56e0\u662f\uff1a\u5982\u679c\u4f60\u6ca1\u6709\u63d0\u4f9b\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u53c8\u6ca1\u6709\u5b9a\u4e49\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u5c06\u4e3a\u4f60\u81ea\u52a8\u751f\u6210\u4e00\u4e2a\uff0c\u7f16\u8bd1\u5668\u751f\u6210\u7684\u6784\u9020\u51fd\u6570\u5e76\u4e0d\u4f1a\u5bf9\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5b9a\u4e49\u7684\u7c7b\u7ee7\u627f\u73b0\u6709\u7c7b\uff0c\u800c\u4f60\u53c8\u6ca1\u6709\u589e\u52a0\u65b0\u7684\u6210\u5458\u53d8\u91cf\uff0c\u5219\u4e0d\u9700\u8981\u4e3a\u65b0\u7c7b\u5b9a\u4e49\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002<\/p>\n<h2>3. \u660e\u786e\u7684\u6784\u9020\u51fd\u6570\uff08Explicit Constructors\uff09<\/h2>\n<p>\u5bf9\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u4f7f\u7528C++\u5173\u952e\u5b57explicit\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u901a\u5e38\uff0c\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u53ef\u88ab\u7528\u4e8e\u8f6c\u6362\uff08conversion\uff0c\u8bd1\u8005\u6ce8\uff1a\u4e3b\u8981\u6307\u9690\u5f0f\u8f6c\u6362\uff0c\u4e0b\u6587\u53ef\u89c1\uff09\uff0c\u4f8b\u5982\uff0c\u5b9a\u4e49\u4e86Foo::Foo(string name)\uff0c\u5f53\u5411\u9700\u8981\u4f20\u5165\u4e00\u4e2aFoo\u5bf9\u8c61\u7684\u51fd\u6570\u4f20\u5165\u4e00\u4e2a\u5b57\u7b26\u4e32\u65f6\uff0c\u6784\u9020\u51fd\u6570Foo::Foo(string name)\u88ab\u8c03\u7528\u5e76\u5c06\u8be5\u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a\u4e00\u4e2aFoo\u4e34\u65f6\u5bf9\u8c61\u4f20\u7ed9\u8c03\u7528\u51fd\u6570\u3002\u770b\u4e0a\u53bb\u5f88\u65b9\u4fbf\uff0c\u4f46\u5982\u679c\u4f60\u5e76\u4e0d\u5e0c\u671b\u5982\u6b64\u901a\u8fc7\u8f6c\u6362\u751f\u6210\u4e00\u4e2a\u65b0\u5bf9\u8c61\u7684\u8bdd\uff0c\u9ebb\u70e6\u4e5f\u968f\u4e4b\u800c\u6765\u3002\u4e3a\u907f\u514d\u6784\u9020\u51fd\u6570\u88ab\u8c03\u7528\u9020\u6210\u9690\u5f0f\u8f6c\u6362\uff0c\u53ef\u4ee5\u5c06\u5176\u58f0\u660e\u4e3aexplicit\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u907f\u514d\u4e0d\u5408\u65f6\u5b9c\u7684\u53d8\u6362\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u65e0\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u6240\u6709\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u5fc5\u987b\u662f\u660e\u786e\u7684\u3002\u5728\u7c7b\u5b9a\u4e49\u4e2d\uff0c\u5c06\u5173\u952e\u5b57explicit\u52a0\u5230\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u524d\uff1aexplicit Foo(string name);<\/p>\n<p>\u4f8b\u5916\uff1a\u5728\u5c11\u6570\u60c5\u51b5\u4e0b\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53ef\u4ee5\u4e0d\u58f0\u660e\u4e3aexplicit\uff1b\u7279\u610f\u4f5c\u4e3a\u5176\u4ed6\u7c7b\u7684\u900f\u660e\u5305\u88c5\u5668\u7684\u7c7b\u3002\u7c7b\u4f3c\u4f8b\u5916\u60c5\u51b5\u5e94\u5728\u6ce8\u91ca\u4e2d\u660e\u786e\u8bf4\u660e\u3002<\/p>\n<h2>4. \u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08Copy Constructors\uff09<\/h2>\n<p>\u4ec5\u5728\u4ee3\u7801\u4e2d\u9700\u8981\u62f7\u8d1d\u4e00\u4e2a\u7c7b\u5bf9\u8c61\u7684\u65f6\u5019\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff1b\u4e0d\u9700\u8981\u62f7\u8d1d\u65f6\u5e94\u4f7f\u7528DISALLOW_COPY_AND_ASSIGN\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u901a\u8fc7\u62f7\u8d1d\u65b0\u5efa\u5bf9\u8c61\u65f6\u53ef\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff08\u7279\u522b\u662f\u5bf9\u8c61\u7684\u4f20\u503c\u65f6\uff09\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u4f7f\u5f97\u62f7\u8d1d\u5bf9\u8c61\u66f4\u52a0\u5bb9\u6613\uff0cSTL\u5bb9\u5668\u8981\u6c42\u6240\u6709\u5185\u5bb9\u53ef\u62f7\u8d1d\u3001\u53ef\u8d4b\u503c\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>C++\u4e2d\u5bf9\u8c61\u7684\u9690\u5f0f\u62f7\u8d1d\u662f\u5bfc\u81f4\u5f88\u591a\u6027\u80fd\u95ee\u9898\u548cbugs\u7684\u6839\u6e90\u3002\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u964d\u4f4e\u4e86\u4ee3\u7801\u53ef\u8bfb\u6027\uff0c\u76f8\u6bd4\u6309\u5f15\u7528\u4f20\u9012\uff0c\u8ddf\u8e2a\u6309\u503c\u4f20\u9012\u7684\u5bf9\u8c61\u66f4\u52a0\u56f0\u96be\uff0c\u5bf9\u8c61\u4fee\u6539\u7684\u5730\u65b9\u53d8\u5f97\u96be\u4ee5\u6349\u6478\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u5927\u91cf\u7684\u7c7b\u5e76\u4e0d\u9700\u8981\u53ef\u62f7\u8d1d\uff0c\u4e5f\u4e0d\u9700\u8981\u4e00\u4e2a\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6216\u8d4b\u503c\u64cd\u4f5c\uff08assignment operator\uff09\u3002\u4e0d\u5e78\u7684\u662f\uff0c\u5982\u679c\u4f60\u4e0d\u4e3b\u52a8\u58f0\u660e\u5b83\u4eec\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u4f60\u81ea\u52a8\u751f\u6210\uff0c\u800c\u4e14\u662fpublic\u7684\u3002<\/p>\n<p>\u53ef\u4ee5\u8003\u8651\u5728\u7c7b\u7684private\u4e2d\u6dfb\u52a0\u7a7a\u7684\uff08dummy\uff09\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\uff0c\u53ea\u6709\u58f0\u660e\uff0c\u6ca1\u6709\u5b9a\u4e49\u3002\u7531\u4e8e\u8fd9\u4e9b\u7a7a\u7a0b\u5e8f\u58f0\u660e\u4e3aprivate\uff0c\u5f53\u5176\u4ed6\u4ee3\u7801\u8bd5\u56fe\u4f7f\u7528\u5b83\u4eec\u7684\u65f6\u5019\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u9519\u3002\u4e3a\u4e86\u65b9\u4fbf\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b8fDISALLOW_COPY_AND_ASSIGN\uff1a<\/p>\n<p>\/\/ \u7981\u6b62\u4f7f\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\u7684\u5b8f<\/p>\n<p>\/\/ \u5e94\u5728\u7c7b\u7684private:\u4e2d\u4f7f\u7528<\/p>\n<p>#define DISALLOW_COPY_AND_ASSIGN(TypeName) \\<\/p>\n<p>TypeName(const TypeName&amp;);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \\<\/p>\n<p>void operator=(const TypeName&amp;)<\/p>\n<p>class Foo {<\/p>\n<p>public:<\/p>\n<p>Foo(int f);<\/p>\n<p>~Foo();<\/p>\n<p>private:<\/p>\n<p>DISALLOW_COPY_AND_ASSIGN(Foo);<\/p>\n<p>};<\/p>\n<p>\u5982\u4e0a\u6240\u8ff0\uff0c\u7edd\u5927\u591a\u6570\u60c5\u51b5\u4e0b\u90fd\u5e94\u4f7f\u7528DISALLOW_COPY_AND_ASSIGN\uff0c\u5982\u679c\u7c7b\u786e\u5b9e\u9700\u8981\u53ef\u62f7\u8d1d\uff0c\u5e94\u5728\u8be5\u7c7b\u7684\u5934\u6587\u4ef6\u4e2d\u8bf4\u660e\u539f\u7531\uff0c\u5e76\u9002\u5f53\u5b9a\u4e49\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u64cd\u4f5c\uff0c\u6ce8\u610f\u5728operator=\u4e2d\u68c0\u6d4b\u81ea\u8d4b\u503c\uff08self-assignment\uff09\u60c5\u51b5\u3002<\/p>\n<p>\u5728\u5c06\u7c7b\u4f5c\u4e3aSTL\u5bb9\u5668\u503c\u5f97\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u6709\u4f7f\u7c7b\u53ef\u62f7\u8d1d\u7684\u51b2\u52a8\u3002\u7c7b\u4f3c\u60c5\u51b5\u4e0b\uff0c\u771f\u6b63\u8be5\u505a\u7684\u662f\u4f7f\u7528\u6307\u9488\u6307\u5411STL\u5bb9\u5668\u4e2d\u7684\u5bf9\u8c61\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528std::tr1::shared_ptr\u3002<\/p>\n<h2>5. \u7ed3\u6784\u4f53\u548c\u7c7b\uff08Structs vs. Classes\uff09<\/h2>\n<p>\u4ec5\u5f53\u53ea\u6709\u6570\u636e\u65f6\u4f7f\u7528struct\uff0c\u5176\u5b83\u4e00\u6982\u4f7f\u7528class\u3002<\/p>\n<p>\u5728C++\u4e2d\uff0c\u5173\u952e\u5b57struct\u548cclass\u51e0\u4e4e\u542b\u4e49\u7b49\u540c\uff0c\u6211\u4eec\u4e3a\u5176\u4eba\u4e3a\u6dfb\u52a0\u8bed\u4e49\uff0c\u4ee5\u4fbf\u4e3a\u5b9a\u4e49\u7684\u6570\u636e\u7c7b\u578b\u5408\u7406\u9009\u62e9\u4f7f\u7528\u54ea\u4e2a\u5173\u952e\u5b57\u3002<\/p>\n<p>struct\u88ab\u7528\u5728\u4ec5\u5305\u542b\u6570\u636e\u7684\u6d88\u6781\u5bf9\u8c61\uff08passive objects\uff09\u4e0a\uff0c\u53ef\u80fd\u5305\u62ec\u6709\u5173\u8054\u7684\u5e38\u91cf\uff0c\u4f46\u6ca1\u6709\u5b58\u53d6\u6570\u636e\u6210\u5458\u4e4b\u5916\u7684\u51fd\u6570\u529f\u80fd\uff0c\u800c\u5b58\u53d6\u529f\u80fd\u901a\u8fc7\u76f4\u63a5\u8bbf\u95ee\u5b9e\u73b0\u800c\u65e0\u9700\u65b9\u6cd5\u8c03\u7528\uff0c\u8fd9\u513f\u63d0\u5230\u7684\u65b9\u6cd5\u662f\u6307\u53ea\u7528\u4e8e\u5904\u7406\u6570\u636e\u6210\u5458\u7684\uff0c\u5982\u6784\u9020\u51fd\u6570\u3001\u6790\u6784\u51fd\u6570\u3001Initialize()\u3001Reset()\u3001Validate()\u3002<\/p>\n<p>\u5982\u679c\u9700\u8981\u66f4\u591a\u7684\u51fd\u6570\u529f\u80fd\uff0cclass\u66f4\u9002\u5408\uff0c\u5982\u679c\u4e0d\u786e\u5b9a\u7684\u8bdd\uff0c\u76f4\u63a5\u4f7f\u7528class\u3002<\/p>\n<p>\u5982\u679c\u4e0eSTL\u7ed3\u5408\uff0c\u5bf9\u4e8e\u4eff\u51fd\u6570\uff08functors\uff09\u548c\u7279\u6027\uff08traits\uff09\u53ef\u4ee5\u4e0d\u7528class\u800c\u662f\u4f7f\u7528struct\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u7c7b\u548c\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53d8\u91cf\u4f7f\u7528\u4e0d\u540c\u7684\u547d\u540d\u89c4\u5219\u3002<\/p>\n<h2>6. \u7ee7\u627f\uff08Inheritance\uff09<\/h2>\n<p>\u4f7f\u7528\u7ec4\u5408\uff08composition\uff0c\u8bd1\u8005\u6ce8\uff0c\u8fd9\u4e00\u70b9\u4e5f\u662fGoF\u5728\u300aDesign Patterns\u300b\u91cc\u53cd\u590d\u5f3a\u8c03\u7684\uff09\u901a\u5e38\u6bd4\u4f7f\u7528\u7ee7\u627f\u66f4\u9002\u5b9c\uff0c\u5982\u679c\u4f7f\u7528\u7ee7\u627f\u7684\u8bdd\uff0c\u53ea\u4f7f\u7528\u516c\u5171\u7ee7\u627f\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u5f53\u5b50\u7c7b\u7ee7\u627f\u57fa\u7c7b\u65f6\uff0c\u5b50\u7c7b\u5305\u542b\u4e86\u7236\u57fa\u7c7b\u6240\u6709\u6570\u636e\u53ca\u64cd\u4f5c\u7684\u5b9a\u4e49\u3002C++\u5b9e\u8df5\u4e2d\uff0c\u7ee7\u627f\u4e3b\u8981\u7528\u4e8e\u4e24\u79cd\u573a\u5408\uff1a\u5b9e\u73b0\u7ee7\u627f\uff08implementation inheritance\uff09\uff0c\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u7684\u5b9e\u73b0\u4ee3\u7801\uff1b\u63a5\u53e3\u7ee7\u627f\uff08interface inheritance\uff09\uff0c\u5b50\u7c7b\u4ec5\u7ee7\u627f\u7236\u7c7b\u7684\u65b9\u6cd5\u540d\u79f0\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u5b9e\u73b0\u7ee7\u627f\u901a\u8fc7\u539f\u5c01\u4e0d\u52a8\u7684\u91cd\u7528\u57fa\u7c7b\u4ee3\u7801\u51cf\u5c11\u4e86\u4ee3\u7801\u91cf\u3002\u7531\u4e8e\u7ee7\u627f\u662f\u7f16\u8bd1\u65f6\u58f0\u660e\uff08compile-time declaration\uff09\uff0c\u7f16\u7801\u8005\u548c\u7f16\u8bd1\u5668\u90fd\u53ef\u4ee5\u7406\u89e3\u76f8\u5e94\u64cd\u4f5c\u5e76\u53d1\u73b0\u9519\u8bef\u3002\u63a5\u53e3\u7ee7\u627f\u53ef\u7528\u4e8e\u7a0b\u5e8f\u4e0a\u589e\u5f3a\u7c7b\u7684\u7279\u5b9aAPI\u7684\u529f\u80fd\uff0c\u5728\u7c7b\u6ca1\u6709\u5b9a\u4e49API\u7684\u5fc5\u8981\u5b9e\u73b0\u65f6\uff0c\u7f16\u8bd1\u5668\u540c\u6837\u53ef\u4ee5\u4fa6\u9519\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5bf9\u4e8e\u5b9e\u73b0\u7ee7\u627f\uff0c\u7531\u4e8e\u5b9e\u73b0\u5b50\u7c7b\u7684\u4ee3\u7801\u5728\u7236\u7c7b\u548c\u5b50\u7c7b\u95f4\u5ef6\u5c55\uff0c\u8981\u7406\u89e3\u5176\u5b9e\u73b0\u53d8\u5f97\u66f4\u52a0\u56f0\u96be\u3002\u5b50\u7c7b\u4e0d\u80fd\u91cd\u5199\u7236\u7c7b\u7684\u975e\u865a\u51fd\u6570\uff0c\u5f53\u7136\u4e5f\u5c31\u4e0d\u80fd\u4fee\u6539\u5176\u5b9e\u73b0\u3002\u57fa\u7c7b\u4e5f\u53ef\u80fd\u5b9a\u4e49\u4e86\u4e00\u4e9b\u6570\u636e\u6210\u5458\uff0c\u8fd8\u8981\u533a\u5206\u57fa\u7c7b\u7684\u7269\u7406\u8f6e\u5ed3\uff08physical layout\uff09\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u6240\u6709\u7ee7\u627f\u5fc5\u987b\u662fpublic\u7684\uff0c\u5982\u679c\u60f3\u79c1\u6709\u7ee7\u627f\u7684\u8bdd\uff0c\u5e94\u8be5\u91c7\u53d6\u5305\u542b\u57fa\u7c7b\u5b9e\u4f8b\u4f5c\u4e3a\u6210\u5458\u7684\u65b9\u5f0f\u4f5c\u4e3a\u66ff\u4ee3\u3002<\/p>\n<p>\u4e0d\u8981\u8fc7\u591a\u4f7f\u7528\u5b9e\u73b0\u7ee7\u627f\uff0c\u7ec4\u5408\u901a\u5e38\u66f4\u5408\u9002\u4e00\u4e9b\u3002\u52aa\u529b\u505a\u5230\u53ea\u5728\u201c\u662f\u4e00\u4e2a\u201d\uff08&#8221;is-a&#8221;\uff0c\u8bd1\u8005\u6ce8\uff0c\u5176\u4ed6&#8221;has-a&#8221;\u60c5\u51b5\u4e0b\u8bf7\u4f7f\u7528\u7ec4\u5408\uff09\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528\u7ee7\u627f\uff1a\u5982\u679cBar\u7684\u786e\u201c\u662f\u4e00\u79cd\u201dFoo\uff0c\u624d\u4ee4Bar\u662fFoo\u7684\u5b50\u7c7b\u3002<\/p>\n<p>\u5fc5\u8981\u7684\u8bdd\uff0c\u4ee4\u6790\u6784\u51fd\u6570\u4e3avirtual\uff0c\u5fc5\u8981\u662f\u6307\uff0c\u5982\u679c\u8be5\u7c7b\u5177\u6709\u865a\u51fd\u6570\uff0c\u5176\u6790\u6784\u51fd\u6570\u5e94\u8be5\u4e3a\u865a\u51fd\u6570\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u81f3\u4e8e\u5b50\u7c7b\u6ca1\u6709\u989d\u5916\u6570\u636e\u6210\u5458\uff0c\u751a\u81f3\u7236\u7c7b\u4e5f\u6ca1\u6709\u4efb\u4f55\u6570\u636e\u6210\u5458\u7684\u7279\u6b8a\u60c5\u51b5\u4e0b\uff0c\u6790\u6784\u51fd\u6570\u7684\u8c03\u7528\u662f\u5426\u5fc5\u8981\u662f\u8bed\u4e49\u4e89\u8bba\uff0c\u4ece\u7f16\u7a0b\u8bbe\u8ba1\u89c4\u8303\u7684\u89d2\u5ea6\u770b\uff0c\u5728\u542b\u6709\u865a\u51fd\u6570\u7684\u7236\u7c7b\u4e2d\uff0c\u5b9a\u4e49\u865a\u6790\u6784\u51fd\u6570\u7edd\u5bf9\u5fc5\u8981\u3002<\/strong><\/p>\n<p>\u9650\u5b9a\u4ec5\u5728\u5b50\u7c7b\u8bbf\u95ee\u7684\u6210\u5458\u51fd\u6570\u4e3aprotected\uff0c\u9700\u8981\u6ce8\u610f\u7684\u662f\u6570\u636e\u6210\u5458\u5e94\u59cb\u7ec8\u4e3a\u79c1\u6709\u3002<\/p>\n<p>\u5f53\u91cd\u5b9a\u4e49\u6d3e\u751f\u7684\u865a\u51fd\u6570\u65f6\uff0c\u5728\u6d3e\u751f\u7c7b\u4e2d\u660e\u786e\u58f0\u660e\u5176\u4e3avirtual\u3002\u6839\u672c\u539f\u56e0\uff1a\u5982\u679c\u9057\u6f0fvirtual\uff0c\u9605\u8bfb\u8005\u9700\u8981\u68c0\u7d22\u7c7b\u7684\u6240\u6709\u7956\u5148\u4ee5\u786e\u5b9a\u8be5\u51fd\u6570\u662f\u5426\u4e3a\u865a\u51fd\u6570\uff08\u8bd1\u8005\u6ce8\uff0c\u867d\u7136\u4e0d\u5f71\u54cd\u5176\u4e3a\u865a\u51fd\u6570\u7684\u672c\u8d28\uff09\u3002<\/p>\n<h2>7. \u591a\u91cd\u7ee7\u627f\uff08Multiple Inheritance\uff09<\/h2>\n<p>\u771f\u6b63\u9700\u8981\u7528\u5230\u591a\u91cd\u5b9e\u73b0\u7ee7\u627f\uff08multiple implementation inheritance\uff09\u7684\u65f6\u5019\u975e\u5e38\u5c11\uff0c\u53ea\u6709\u5f53\u6700\u591a\u4e00\u4e2a\u57fa\u7c7b\u4e2d\u542b\u6709\u5b9e\u73b0\uff0c\u5176\u4ed6\u57fa\u7c7b\u90fd\u662f\u4ee5Interface\u4e3a\u540e\u7f00\u7684\u7eaf\u63a5\u53e3\u7c7b\u65f6\u624d\u4f1a\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u591a\u91cd\u7ee7\u627f\u5141\u8bb8\u5b50\u7c7b\u62e5\u6709\u591a\u4e2a\u57fa\u7c7b\uff0c\u8981\u5c06\u4f5c\u4e3a\u7eaf\u63a5\u53e3\u7684\u57fa\u7c7b\u548c\u5177\u6709\u5b9e\u73b0\u7684\u57fa\u7c7b\u533a\u522b\u5f00\u6765\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u76f8\u6bd4\u5355\u7ee7\u627f\uff0c\u591a\u91cd\u5b9e\u73b0\u7ee7\u627f\u53ef\u4ee4\u4f60\u91cd\u7528\u66f4\u591a\u4ee3\u7801\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u771f\u6b63\u9700\u8981\u7528\u5230\u591a\u91cd\u5b9e\u73b0\u7ee7\u627f\u7684\u65f6\u5019\u975e\u5e38\u5c11\uff0c\u591a\u91cd\u5b9e\u73b0\u7ee7\u627f\u770b\u4e0a\u53bb\u662f\u4e0d\u9519\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u901a\u5e38\u53ef\u4ee5\u627e\u5230\u66f4\u52a0\u660e\u786e\u3001\u6e05\u6670\u7684\u3001\u4e0d\u540c\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u53ea\u6709\u5f53\u6240\u6709\u8d85\u7c7b\uff08superclass\uff09\u9664\u7b2c\u4e00\u4e2a\u5916\u90fd\u662f\u7eaf\u63a5\u53e3\u65f6\u624d\u80fd\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\u3002\u4e3a\u786e\u4fdd\u5b83\u4eec\u662f\u7eaf\u63a5\u53e3\uff0c\u8fd9\u4e9b\u7c7b\u5fc5\u987b\u4ee5Interface\u4e3a\u540e\u7f00\u3002<\/p>\n<p>\u6ce8\u610f\uff1a\u5173\u4e8e\u6b64\u89c4\u5219\uff0cWindows\u4e0b\u6709\u79cd\u4f8b\u5916\u60c5\u51b5\uff08\u8bd1\u8005\u6ce8\uff0c\u5c06\u5728\u672c\u8bd1\u6587\u6700\u540e\u4e00\u7bc7\u7684\u89c4\u5219\u4f8b\u5916\u4e2d\u9610\u8ff0\uff09\u3002<\/p>\n<h2>8. \u63a5\u53e3\uff08Interface\uff09<\/h2>\n<p>\u63a5\u53e3\u662f\u6307\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u7c7b\uff0c\u8fd9\u4e9b\u7c7b\u4ee5Interface\u4e3a\u540e\u7f00\uff08\u975e\u5fc5\u9700\uff09\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u5f53\u4e00\u4e2a\u7c7b\u6ee1\u8db3\u4ee5\u4e0b\u8981\u6c42\u65f6\uff0c\u79f0\u4e4b\u4e3a\u7eaf\u63a5\u53e3\uff1a<\/p>\n<p>1) \u53ea\u6709\u7eaf\u865a\u51fd\u6570\uff08&#8221;=0&#8243;\uff09\u548c\u9759\u6001\u51fd\u6570\uff08\u4e0b\u6587\u63d0\u5230\u7684\u6790\u6784\u51fd\u6570\u9664\u5916\uff09\uff1b<\/p>\n<p>2) \u6ca1\u6709\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff1b<\/p>\n<p>3) \u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u6784\u9020\u51fd\u6570\u3002\u5982\u679c\u6709\uff0c\u4e5f\u4e0d\u542b\u53c2\u6570\uff0c\u5e76\u4e14\u4e3aprotected\uff1b<\/p>\n<p>4) \u5982\u679c\u662f\u5b50\u7c7b\uff0c\u4e5f\u53ea\u80fd\u7ee7\u627f\u6ee1\u8db3\u4e0a\u8ff0\u6761\u4ef6\u5e76\u4ee5Interface\u4e3a\u540e\u7f00\u7684\u7c7b\u3002<\/p>\n<p>\u63a5\u53e3\u7c7b\u4e0d\u80fd\u88ab\u76f4\u63a5\u5b9e\u4f8b\u5316\uff0c\u56e0\u4e3a\u5b83\u58f0\u660e\u4e86\u7eaf\u865a\u51fd\u6570\u3002\u4e3a\u786e\u4fdd\u63a5\u53e3\u7c7b\u7684\u6240\u6709\u5b9e\u73b0\u53ef\u88ab\u6b63\u786e\u9500\u6bc1\uff0c\u5fc5\u987b\u4e3a\u4e4b\u58f0\u660e\u865a\u6790\u6784\u51fd\u6570\uff08\u4f5c\u4e3a\u7b2c1\u6761\u89c4\u5219\u7684\u4f8b\u5916\uff0c\u6790\u6784\u51fd\u6570\u4e0d\u80fd\u662f\u7eaf\u865a\u51fd\u6570\uff09\u3002\u5177\u4f53\u7ec6\u8282\u53ef\u53c2\u8003Stroustrup\u7684\u300a<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/16\/www.amazon.com\/Programming-Language-3rd-Bjarne-Stroustrup\/dp\/0201889544\">The C++ Programming Language, 3rd edition<\/a>\u300b\u7b2c12.4\u8282\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u4ee5Interface\u4e3a\u540e\u7f00\u53ef\u4ee4\u4ed6\u4eba\u77e5\u9053\u4e0d\u80fd\u4e3a\u8be5\u63a5\u53e3\u7c7b\u589e\u52a0\u5b9e\u73b0\u51fd\u6570\u6216\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff0c\u8fd9\u4e00\u70b9\u5bf9\u4e8e\u591a\u91cd\u7ee7\u627f\u5c24\u5176\u91cd\u8981\u3002\u53e6\u5916\uff0c\u5bf9\u4e8eJava\u7a0b\u5e8f\u5458\u6765\u8bf4\uff0c\u63a5\u53e3\u7684\u6982\u5ff5\u5df2\u7ecf\u6df1\u5165\u4eba\u5fc3\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>Interface\u540e\u7f00\u589e\u52a0\u4e86\u7c7b\u540d\u957f\u5ea6\uff0c\u4e3a\u9605\u8bfb\u548c\u7406\u89e3\u5e26\u6765\u4e0d\u4fbf\uff0c\u540c\u65f6\uff0c\u63a5\u53e3\u7279\u6027\u4f5c\u4e3a\u5b9e\u73b0\u7ec6\u8282\u4e0d\u5e94\u66b4\u9732\u7ed9\u5ba2\u6237\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u3002\u53ea\u6709\u5728\u6ee1\u8db3\u4e0a\u8ff0\u9700\u8981\u65f6\uff0c\u7c7b\u624d\u4ee5Interface\u7ed3\u5c3e\uff0c\u4f46\u53cd\u8fc7\u6765\uff0c\u6ee1\u8db3\u4e0a\u8ff0\u9700\u8981\u7684\u7c7b\u672a\u5fc5\u4e00\u5b9a\u4ee5Interface\u7ed3\u5c3e\u3002<\/p>\n<h2>9. \u64cd\u4f5c\u7b26\u91cd\u8f7d\uff08Operator Overloading\uff09<\/h2>\n<p>\u9664\u5c11\u6570\u7279\u5b9a\u73af\u5883\u5916\uff0c\u4e0d\u8981\u91cd\u8f7d\u64cd\u4f5c\u7b26\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u4e00\u4e2a\u7c7b\u53ef\u4ee5\u5b9a\u4e49\u8bf8\u5982+\u3001\/\u7b49\u64cd\u4f5c\u7b26\uff0c\u4f7f\u5176\u53ef\u4ee5\u50cf\u5185\u5efa\u7c7b\u578b\u4e00\u6837\u76f4\u63a5\u4f7f\u7528\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u4f7f\u4ee3\u7801\u770b\u4e0a\u53bb\u66f4\u52a0\u76f4\u89c2\uff0c\u5c31\u50cf\u5185\u5efa\u7c7b\u578b\uff08\u5982int\uff09\u90a3\u6837\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4f7f\u90a3\u4e9bEquals()\u3001Add()\u7b49\u9eef\u6de1\u65e0\u5149\u7684\u51fd\u6570\u540d\u597d\u73a9\u591a\u4e86\u3002\u4e3a\u4e86\u4f7f\u4e00\u4e9b\u6a21\u677f\u51fd\u6570\u6b63\u786e\u5de5\u4f5c\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5b9a\u4e49\u64cd\u4f5c\u7b26\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u867d\u7136\u64cd\u4f5c\u7b26\u91cd\u8f7d\u4ee4\u4ee3\u7801\u66f4\u52a0\u76f4\u89c2\uff0c\u4f46\u4e5f\u6709\u4e00\u4e9b\u4e0d\u8db3<\/p>\n<p>1) \u6df7\u6dc6\u76f4\u89c9\uff0c\u8ba9\u4f60\u8bef\u4ee5\u4e3a\u4e00\u4e9b\u8017\u65f6\u7684\u64cd\u4f5c\u50cf\u5185\u5efa\u64cd\u4f5c\u90a3\u6837\u8f7b\u5de7\uff1b<\/p>\n<p>2) \u67e5\u627e\u91cd\u8f7d\u64cd\u4f5c\u7b26\u7684\u8c03\u7528\u5904\u66f4\u52a0\u56f0\u96be\uff0c\u67e5\u627eEquals()\u663e\u7136\u6bd4\u540c\u7b49\u8c03\u7528==\u5bb9\u6613\u7684\u591a\uff1b<\/p>\n<p>3) \u6709\u7684\u64cd\u4f5c\u7b26\u53ef\u4ee5\u5bf9\u6307\u9488\u8fdb\u884c\u64cd\u4f5c\uff0c\u5bb9\u6613\u5bfc\u81f4bugs\uff0cFoo + 4\u505a\u7684\u662f\u4e00\u4ef6\u4e8b\uff0c\u800c&amp;Foo + 4\u53ef\u80fd\u505a\u7684\u662f\u5b8c\u5168\u4e0d\u540c\u7684\u53e6\u4e00\u4ef6\u4e8b\uff0c\u5bf9\u4e8e\u4e8c\u8005\uff0c\u7f16\u8bd1\u5668\u90fd\u4e0d\u4f1a\u62a5\u9519\uff0c\u4f7f\u5176\u5f88\u96be\u8c03\u8bd5\uff1b<\/p>\n<p>4) \u91cd\u8f7d\u8fd8\u6709\u4ee4\u4f60\u5403\u60ca\u7684\u526f\u4f5c\u7528\uff0c\u6bd4\u5982\uff0c\u91cd\u8f7d\u64cd\u4f5c\u7b26&amp;\u7684\u7c7b\u4e0d\u80fd\u88ab\u524d\u7f6e\u58f0\u660e\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u4e00\u822c\u4e0d\u8981\u91cd\u8f7d\u64cd\u4f5c\u7b26\uff0c\u5c24\u5176\u662f\u8d4b\u503c\u64cd\u4f5c\uff08operator=\uff09\u6bd4\u8f83\u9634\u9669\uff0c\u5e94\u907f\u514d\u91cd\u8f7d\u3002\u5982\u679c\u9700\u8981\u7684\u8bdd\uff0c\u53ef\u4ee5\u5b9a\u4e49\u7c7b\u4f3cEquals()\u3001CopyFrom()\u7b49\u51fd\u6570\u3002<\/p>\n<p>\u7136\u800c\uff0c\u6781\u5c11\u6570\u60c5\u51b5\u4e0b\u9700\u8981\u91cd\u8f7d\u64cd\u4f5c\u7b26\u4ee5\u4fbf\u4e0e\u6a21\u677f\u6216\u201c\u6807\u51c6\u201dC++\u7c7b\u8854\u63a5\uff08\u5982operator&lt;&lt;(ostream&amp;, const T&amp;)\uff09\uff0c\u5982\u679c\u88ab\u8bc1\u660e\u662f\u6b63\u5f53\u7684\u5c1a\u53ef\u63a5\u53d7\uff0c\u4f46\u4f60\u8981\u5c3d\u53ef\u80fd\u907f\u514d\u8fd9\u6837\u505a\u3002\u5c24\u5176\u662f\u4e0d\u8981\u4ec5\u4ec5\u4e3a\u4e86\u5728STL\u5bb9\u5668\u4e2d\u4f5c\u4e3akey\u4f7f\u7528\u5c31\u91cd\u8f7doperator==\u6216operator&lt;\uff0c\u53d6\u800c\u4ee3\u4e4b\uff0c\u4f60\u5e94\u8be5\u5728\u58f0\u660e\u5bb9\u5668\u7684\u65f6\u5019\uff0c\u521b\u5efa\u76f8\u7b49\u5224\u65ad\u548c\u5927\u5c0f\u6bd4\u8f83\u7684\u4eff\u51fd\u6570\u7c7b\u578b\u3002<\/p>\n<p>\u6709\u4e9bSTL\u7b97\u6cd5\u786e\u5b9e\u9700\u8981\u91cd\u8f7doperator==\u65f6\u53ef\u4ee5\u8fd9\u4e48\u505a\uff0c\u4e0d\u8981\u5fd8\u4e86\u63d0\u4f9b\u6587\u6863\u8bf4\u660e\u539f\u56e0\u3002<\/p>\n<p>\u53c2\u8003<strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/strong>\u548c<strong>\u51fd\u6570\u91cd\u8f7d<\/strong>\u3002<\/p>\n<h2>10. \u5b58\u53d6\u63a7\u5236\uff08Access Control\uff09<\/h2>\n<p>\u5c06\u6570\u636e\u6210\u5458\u79c1\u6709\u5316\uff0c\u5e76\u63d0\u4f9b\u76f8\u5173\u5b58\u53d6\u51fd\u6570\uff0c\u5982\u5b9a\u4e49\u53d8\u91cffoo_\u53ca\u53d6\u503c\u51fd\u6570foo()\u3001\u8d4b\u503c\u51fd\u6570set_foo()\u3002<\/p>\n<p>\u5b58\u53d6\u51fd\u6570\u7684\u5b9a\u4e49\u4e00\u822c\u5185\u8054\u5728\u5934\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u53c2\u8003<strong>\u7ee7\u627f<\/strong>\u548c<strong>\u51fd\u6570\u547d\u540d<\/strong>\u3002<\/p>\n<h2>11. \u58f0\u660e\u6b21\u5e8f\uff08Declaration Order\uff09<\/h2>\n<p>\u5728\u7c7b\u4e2d\u4f7f\u7528\u7279\u5b9a\u7684\u58f0\u660e\u6b21\u5e8f\uff1apublic:\u5728private:\u4e4b\u524d\uff0c\u6210\u5458\u51fd\u6570\u5728\u6570\u636e\u6210\u5458\uff08\u53d8\u91cf\uff09\u524d\u3002<\/p>\n<p>\u5b9a\u4e49\u6b21\u5e8f\u5982\u4e0b\uff1apublic:\u3001protected:\u3001private:\uff0c\u5982\u679c\u90a3\u4e00\u5757\u6ca1\u6709\uff0c\u76f4\u63a5\u5ffd\u7565\u5373\u53ef\u3002<\/p>\n<p>\u6bcf\u4e00\u5757\u4e2d\uff0c\u58f0\u660e\u6b21\u5e8f\u4e00\u822c\u5982\u4e0b\uff1a<\/p>\n<p>1) typedefs\u548cenums\uff1b<\/p>\n<p>2) \u5e38\u91cf\uff1b<\/p>\n<p>3) \u6784\u9020\u51fd\u6570\uff1b<\/p>\n<p>4) \u6790\u6784\u51fd\u6570\uff1b<\/p>\n<p>5) \u6210\u5458\u51fd\u6570\uff0c\u542b\u9759\u6001\u6210\u5458\u51fd\u6570\uff1b<\/p>\n<p>6) \u6570\u636e\u6210\u5458\uff0c\u542b\u9759\u6001\u6570\u636e\u6210\u5458\u3002<\/p>\n<p>\u5b8fDISALLOW_COPY_AND_ASSIGN\u7f6e\u4e8eprivate:\u5757\u4e4b\u540e\uff0c\u4f5c\u4e3a\u7c7b\u7684\u6700\u540e\u90e8\u5206\u3002\u53c2\u8003<strong>\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/strong>\u3002<\/p>\n<p>.cc\u6587\u4ef6\u4e2d\u51fd\u6570\u7684\u5b9a\u4e49\u5e94\u5c3d\u53ef\u80fd\u548c\u58f0\u660e\u6b21\u5e8f\u4e00\u81f4\u3002<\/p>\n<p>\u4e0d\u8981\u5c06\u5927\u578b\u51fd\u6570\u5185\u8054\u5230\u7c7b\u7684\u5b9a\u4e49\u4e2d\uff0c\u901a\u5e38\uff0c\u53ea\u6709\u90a3\u4e9b\u6ca1\u6709\u7279\u522b\u610f\u4e49\u7684\u6216\u8005\u6027\u80fd\u8981\u6c42\u9ad8\u7684\uff0c\u5e76\u4e14\u662f\u6bd4\u8f83\u77ed\u5c0f\u7684\u51fd\u6570\u624d\u88ab\u5b9a\u4e49\u4e3a\u5185\u8054\u51fd\u6570\u3002\u66f4\u591a\u7ec6\u8282\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/10\/55818.html\">\u8bd1\u6587\u7b2c\u4e00\u7bc7\u7684<\/a><strong>\u5185\u8054\u51fd\u6570<\/strong>\u3002<\/p>\n<h2>12. \u7f16\u5199\u77ed\u5c0f\u51fd\u6570\uff08Write Short Functions\uff09<\/h2>\n<p>\u503e\u5411\u4e8e\u9009\u62e9\u77ed\u5c0f\u3001\u51dd\u7ec3\u7684\u51fd\u6570\u3002<\/p>\n<p>\u957f\u51fd\u6570\u6709\u65f6\u662f\u6070\u5f53\u7684\uff0c\u56e0\u6b64\u5bf9\u4e8e\u51fd\u6570\u957f\u5ea6\u5e76\u6ca1\u6709\u4e25\u683c\u9650\u5236\u3002\u5982\u679c\u51fd\u6570\u8d85\u8fc740\u884c\uff0c\u53ef\u4ee5\u8003\u8651\u5728\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7ed3\u6784\u7684\u60c5\u51b5\u4e0b\u5c06\u5176\u5206\u5272\u4e00\u4e0b\u3002<\/p>\n<p>\u5373\u4f7f\u4e00\u4e2a\u957f\u51fd\u6570\u73b0\u5728\u5de5\u4f5c\u7684\u975e\u5e38\u597d\uff0c\u4e00\u65e6\u6709\u4eba\u5bf9\u5176\u4fee\u6539\uff0c\u6709\u53ef\u80fd\u51fa\u73b0\u65b0\u7684\u95ee\u9898\uff0c\u751a\u81f3\u5bfc\u81f4\u96be\u4ee5\u53d1\u73b0\u7684bugs\u3002\u4f7f\u51fd\u6570\u5c3d\u91cf\u77ed\u5c0f\u3001\u7b80\u5355\uff0c\u4fbf\u4e8e\u4ed6\u4eba\u9605\u8bfb\u548c\u4fee\u6539\u4ee3\u7801\u3002<\/p>\n<p>\u5728\u5904\u7406\u4ee3\u7801\u65f6\uff0c\u4f60\u53ef\u80fd\u4f1a\u53d1\u73b0\u590d\u6742\u7684\u957f\u51fd\u6570\uff0c\u4e0d\u8981\u5bb3\u6015\u4fee\u6539\u73b0\u6709\u4ee3\u7801\uff1a\u5982\u679c\u8bc1\u5b9e\u8fd9\u4e9b\u4ee3\u7801\u4f7f\u7528\u3001\u8c03\u8bd5\u56f0\u96be\uff0c\u6216\u8005\u4f60\u9700\u8981\u4f7f\u7528\u5176\u4e2d\u7684\u4e00\u5c0f\u5757\uff0c\u8003\u8651\u5c06\u5176\u5206\u5272\u4e3a\u66f4\u52a0\u77ed\u5c0f\u3001\u6613\u4e8e\u7ba1\u7406\u7684\u82e5\u5e72\u51fd\u6570\u3002<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u5173\u4e8e\u7c7b\u7684\u6ce8\u610f\u4e8b\u9879\uff0c\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u4e0d\u5728\u6784\u9020\u51fd\u6570\u4e2d\u505a\u592a\u591a\u903b\u8f91\u76f8\u5173\u7684\u521d\u59cb\u5316\uff1b<\/strong><\/li>\n<li><strong>\u7f16\u8bd1\u5668\u63d0\u4f9b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4e0d\u4f1a\u5bf9\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5982\u679c\u5b9a\u4e49\u4e86\u5176\u4ed6\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4e0d\u518d\u63d0\u4f9b\uff0c\u9700\u8981\u7f16\u7801\u8005\u81ea\u884c\u63d0\u4f9b\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff1b<\/strong><\/li>\n<li><strong>\u4e3a\u907f\u514d\u9690\u5f0f\u8f6c\u6362\uff0c\u9700\u5c06\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u58f0\u660e\u4e3a<\/strong><strong>explicit<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u4e3a\u907f\u514d\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3001\u8d4b\u503c\u64cd\u4f5c\u7684\u6ee5\u7528\u548c\u7f16\u8bd1\u5668\u81ea\u52a8\u751f\u6210\uff0c\u53ef\u76ee\u524d\u58f0\u660e\u5176\u4e3a<\/strong><strong>private<\/strong><strong>\u4e14\u65e0\u9700\u5b9e\u73b0\uff1b<\/strong><\/li>\n<li><strong>\u4ec5\u5728\u4f5c\u4e3a\u6570\u636e\u96c6\u5408\u65f6\u4f7f\u7528<\/strong><strong>struct<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u7ec4\u5408<\/strong><strong>&gt;<\/strong><strong>\u5b9e\u73b0\u7ee7\u627f<\/strong><strong>&gt;<\/strong><strong>\u63a5\u53e3\u7ee7\u627f<\/strong><strong>&gt;<\/strong><strong>\u79c1\u6709\u7ee7\u627f\uff0c\u5b50\u7c7b\u91cd\u8f7d\u7684\u865a\u51fd\u6570\u4e5f\u8981\u58f0\u660e<\/strong><strong>virtual<\/strong><strong>\u5173\u952e\u5b57\uff0c\u867d\u7136\u7f16\u8bd1\u5668\u5141\u8bb8\u4e0d\u8fd9\u6837\u505a\uff1b<\/strong><\/li>\n<li><strong>\u907f\u514d\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4f7f\u7528\u65f6\uff0c\u9664\u4e00\u4e2a\u57fa\u7c7b\u542b\u6709\u5b9e\u73b0\u5916\uff0c\u5176\u4ed6\u57fa\u7c7b\u5747\u4e3a\u7eaf\u63a5\u53e3\uff1b<\/strong><\/li>\n<li><strong>\u63a5\u53e3\u7c7b\u7c7b\u540d\u4ee5<\/strong><strong>Interface<\/strong><strong>\u4e3a\u540e\u7f00\uff0c\u9664\u63d0\u4f9b\u5e26\u5b9e\u73b0\u7684\u865a\u6790\u6784\u51fd\u6570\u3001\u9759\u6001\u6210\u5458\u51fd\u6570\u5916\uff0c\u5176\u4ed6\u5747\u4e3a\u7eaf\u865a\u51fd\u6570\uff0c\u4e0d\u5b9a\u4e49\u975e\u9759\u6001\u6570\u636e\u6210\u5458\uff0c\u4e0d\u63d0\u4f9b\u6784\u9020\u51fd\u6570\uff0c\u63d0\u4f9b\u7684\u8bdd\uff0c\u58f0\u660e\u4e3a<\/strong><strong>protected<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u4e3a\u964d\u4f4e\u590d\u6742\u6027\uff0c\u5c3d\u91cf\u4e0d\u91cd\u8f7d\u64cd\u4f5c\u7b26\uff0c\u6a21\u677f\u3001\u6807\u51c6\u7c7b\u4e2d\u4f7f\u7528\u65f6\u63d0\u4f9b\u6587\u6863\u8bf4\u660e\uff1b<\/strong><\/li>\n<li><strong>\u5b58\u53d6\u51fd\u6570\u4e00\u822c\u5185\u8054\u5728\u5934\u6587\u4ef6\u4e2d\uff1b<\/strong><\/li>\n<li><strong>\u58f0\u660e\u6b21\u5e8f\uff1a<\/strong><strong>public-&gt;protected-&gt;private<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u51fd\u6570\u4f53\u5c3d\u91cf\u77ed\u5c0f\u3001\u7d27\u51d1\uff0c\u529f\u80fd\u5355\u4e00\u3002<\/strong><\/li>\n<\/ol>\n<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/21\/56760.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u56db\uff09<\/a><\/h1>\n<h2>Google\u7279\u6709\u7684\u98ce\u60c5<\/h2>\n<p>Google\u6709\u5f88\u591a\u81ea\u5df1\u5b9e\u73b0\u7684\u4f7fC++\u4ee3\u7801\u66f4\u52a0\u5065\u58ee\u7684\u6280\u5de7\u3001\u529f\u80fd\uff0c\u4ee5\u53ca\u6709\u5f02\u4e8e\u522b\u5904\u7684C++\u7684\u4f7f\u7528\u65b9\u5f0f\u3002<\/p>\n<h2>1. \u667a\u80fd\u6307\u9488\uff08Smart Pointers\uff09<\/h2>\n<p>\u5982\u679c\u786e\u5b9e\u9700\u8981\u4f7f\u7528\u667a\u80fd\u6307\u9488\u7684\u8bdd\uff0cscoped_ptr\u5b8c\u5168\u53ef\u4ee5\u80dc\u4efb\u3002\u5728\u975e\u5e38\u7279\u6b8a\u7684\u60c5\u51b5\u4e0b\uff0c\u4f8b\u5982\u5bf9STL\u5bb9\u5668\u4e2d\u5bf9\u8c61\uff0c\u4f60\u5e94\u8be5\u53ea\u4f7f\u7528std::tr1::shared_ptr\uff0c\u4efb\u4f55\u60c5\u51b5\u4e0b\u90fd\u4e0d\u8981\u4f7f\u7528auto_ptr\u3002<\/p>\n<p>\u201c\u667a\u80fd\u201d\u6307\u9488\u770b\u4e0a\u53bb\u662f\u6307\u9488\uff0c\u5176\u5b9e\u662f\u9644\u52a0\u4e86\u8bed\u4e49\u7684\u5bf9\u8c61\u3002\u4ee5scoped_ptr\u4e3a\u4f8b\uff0cscoped_ptr\u88ab\u9500\u6bc1\u65f6\uff0c\u5220\u9664\u4e86\u5b83\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002shared_ptr\u4e5f\u662f\u5982\u6b64\uff0c\u800c\u4e14\uff0cshared_ptr\u5b9e\u73b0\u4e86\u5f15\u7528\u8ba1\u6570\uff08reference-counting\uff09\uff0c\u4ece\u800c\u53ea\u6709\u5f53\u5b83\u6240\u6307\u5411\u7684\u6700\u540e\u4e00\u4e2a\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\uff0c\u6307\u9488\u624d\u4f1a\u88ab\u5220\u9664\u3002<\/p>\n<p>\u4e00\u822c\u6765\u8bf4\uff0c\u6211\u4eec\u503e\u5411\u4e8e\u8bbe\u8ba1\u5bf9\u8c61\u96b6\u5c5e\u660e\u786e\u7684\u4ee3\u7801\uff0c\u6700\u660e\u786e\u7684\u5bf9\u8c61\u96b6\u5c5e\u662f\u6839\u672c\u4e0d\u4f7f\u7528\u6307\u9488\uff0c\u76f4\u63a5\u5c06\u5bf9\u8c61\u4f5c\u4e3a\u4e00\u4e2a\u57df\uff08field\uff09\u6216\u5c40\u90e8\u53d8\u91cf\u4f7f\u7528\u3002\u53e6\u4e00\u79cd\u6781\u7aef\u662f\u5f15\u7528\u8ba1\u6570\u6307\u9488\u4e0d\u5c5e\u4e8e\u4efb\u4f55\u5bf9\u8c61\uff0c\u8fd9\u6837\u8bbe\u8ba1\u7684\u95ee\u9898\u662f\u5bb9\u6613\u5bfc\u81f4\u5faa\u73af\u5f15\u7528\u6216\u5176\u4ed6\u5bfc\u81f4\u5bf9\u8c61\u65e0\u6cd5\u5220\u9664\u7684\u8be1\u5f02\u6761\u4ef6\uff0c\u800c\u4e14\u5728\u6bcf\u4e00\u6b21\u62f7\u8d1d\u6216\u8d4b\u503c\u65f6\u8fde\u539f\u5b50\u64cd\u4f5c\u90fd\u4f1a\u5f88\u6162\u3002<\/p>\n<p>\u867d\u7136\u4e0d\u63a8\u8350\u8fd9\u4e48\u505a\uff0c\u4f46\u6709\u4e9b\u65f6\u5019\uff0c\u5f15\u7528\u8ba1\u6570\u6307\u9488\u662f\u6700\u7b80\u5355\u6709\u6548\u7684\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u770b\u6765\uff0c<\/strong><strong>Google<\/strong><strong>\u6240\u8c13\u7684\u4e0d\u540c\u4e4b\u5904\uff0c\u5728\u4e8e\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u667a\u80fd\u6307\u9488<\/strong><strong>\ud83d\ude00<\/strong><strong>\uff0c\u4f7f\u7528\u65f6\u4e5f\u5c3d\u91cf\u5c40\u90e8\u5316\uff0c\u5e76\u4e14\uff0c\u5b89\u5168\u7b2c\u4e00\u3002<\/strong><\/p>\n<h2>\u5176\u4ed6C++\u7279\u6027<\/h2>\n<h2>1. \u5f15\u7528\u53c2\u6570\uff08Reference Arguments\uff09<\/h2>\n<p>\u6240\u4ee5\u6309\u5f15\u7528\u4f20\u9012\u7684\u53c2\u6570\u5fc5\u987b\u52a0\u4e0aconst\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u5728C\u8bed\u8a00\u4e2d\uff0c\u5982\u679c\u51fd\u6570\u9700\u8981\u4fee\u6539\u53d8\u91cf\u7684\u503c\uff0c\u5f62\u53c2\uff08parameter\uff09\u5fc5\u987b\u4e3a\u6307\u9488\uff0c\u5982int foo(int *pval)\u3002\u5728C++\u4e2d\uff0c\u51fd\u6570\u8fd8\u53ef\u4ee5\u58f0\u660e\u5f15\u7528\u5f62\u53c2\uff1aint foo(int &amp;val)\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u5b9a\u4e49\u5f62\u53c2\u4e3a\u5f15\u7528\u907f\u514d\u4e86\u50cf(*pval)++\u8fd9\u6837\u4e11\u964b\u7684\u4ee3\u7801\uff0c\u50cf\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8fd9\u6837\u7684\u5e94\u7528\u4e5f\u662f\u5fc5\u9700\u7684\uff0c\u800c\u4e14\u4e0d\u50cf\u6307\u9488\u90a3\u6837\u4e0d\u63a5\u53d7\u7a7a\u6307\u9488NULL\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5bb9\u6613\u5f15\u8d77\u8bef\u89e3\uff0c\u56e0\u4e3a\u5f15\u7528\u5728\u8bed\u6cd5\u4e0a\u662f\u503c\u5374\u62e5\u6709\u6307\u9488\u7684\u8bed\u4e49\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u51fd\u6570\u5f62\u53c2\u8868\u4e2d\uff0c\u6240\u6709\u5f15\u7528\u5fc5\u987b\u662fconst\uff1a<\/p>\n<p>void Foo(const string &amp;in, string *out);<\/p>\n<p>\u4e8b\u5b9e\u4e0a\u8fd9\u662f\u4e00\u4e2a\u786c\u6027\u7ea6\u5b9a\uff1a\u8f93\u5165\u53c2\u6570\u4e3a\u503c\u6216\u5e38\u6570\u5f15\u7528\uff0c\u8f93\u51fa\u53c2\u6570\u4e3a\u6307\u9488\uff1b\u8f93\u5165\u53c2\u6570\u53ef\u4ee5\u662f\u5e38\u6570\u6307\u9488\uff0c\u4f46\u4e0d\u80fd\u4f7f\u7528\u975e\u5e38\u6570\u5f15\u7528\u5f62\u53c2\u3002<\/p>\n<p>\u5728\u5f3a\u8c03\u53c2\u6570\u4e0d\u662f\u62f7\u8d1d\u800c\u6765\uff0c\u5728\u5bf9\u8c61\u751f\u547d\u671f\u5185\u5fc5\u987b\u4e00\u76f4\u5b58\u5728\u65f6\u53ef\u4ee5\u4f7f\u7528\u5e38\u6570\u6307\u9488\uff0c\u6700\u597d\u5c06\u8fd9\u4e9b\u5728\u6ce8\u91ca\u4e2d\u8be6\u7ec6\u8bf4\u660e\u3002bind2nd\u548cmem_fun\u7b49STL\u9002\u914d\u5668\u4e0d\u63a5\u53d7\u5f15\u7528\u5f62\u53c2\uff0c\u8fd9\u79cd\u60c5\u51b5\u4e0b\u4e5f\u5fc5\u987b\u4ee5\u6307\u9488\u5f62\u53c2\u58f0\u660e\u51fd\u6570\u3002<\/p>\n<h2>2. \u51fd\u6570\u91cd\u8f7d\uff08Function Overloading\uff09<\/h2>\n<p>\u4ec5\u5728\u8f93\u5165\u53c2\u6570\u7c7b\u578b\u4e0d\u540c\u3001\u529f\u80fd\u76f8\u540c\u65f6\u4f7f\u7528\u91cd\u8f7d\u51fd\u6570\uff08\u542b\u6784\u9020\u51fd\u6570\uff09\uff0c\u4e0d\u8981\u4f7f\u7528\u51fd\u6570\u91cd\u8f7d\u6a21\u4eff\u7f3a\u7701\u51fd\u6570\u53c2\u6570\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u53ef\u4ee5\u5b9a\u4e49\u4e00\u4e2a\u51fd\u6570\u53c2\u6570\u7c7b\u578b\u4e3aconst string&amp;\uff0c\u5e76\u5b9a\u4e49\u5176\u91cd\u8f7d\u51fd\u6570\u7c7b\u578b\u4e3aconst char*\u3002<\/p>\n<p>class MyClass {<\/p>\n<p>public:<\/p>\n<p>void Analyze(const string &amp;text);<\/p>\n<p>void Analyze(const char *text, size_t textlen);<\/p>\n<p>};<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u901a\u8fc7\u91cd\u8f7d\u4e0d\u540c\u53c2\u6570\u7684\u540c\u540d\u51fd\u6570\uff0c\u4ee4\u4ee3\u7801\u66f4\u52a0\u76f4\u89c2\uff0c\u6a21\u677f\u5316\u4ee3\u7801\u9700\u8981\u91cd\u8f7d\uff0c\u540c\u65f6\u4e3a\u8bbf\u95ee\u8005\u5e26\u6765\u4fbf\u5229\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u9650\u5236\u4f7f\u7528\u91cd\u8f7d\u7684\u4e00\u4e2a\u539f\u56e0\u662f\u5728\u7279\u5b9a\u8c03\u7528\u5904\u5f88\u96be\u786e\u5b9a\u5230\u5e95\u8c03\u7528\u7684\u662f\u54ea\u4e2a\u51fd\u6570\uff0c\u53e6\u4e00\u4e2a\u539f\u56e0\u662f\u5f53\u6d3e\u751f\u7c7b\u53ea\u91cd\u8f7d\u51fd\u6570\u7684\u90e8\u5206\u53d8\u91cf\u4f1a\u4ee4\u5f88\u591a\u4eba\u5bf9\u7ee7\u627f\u8bed\u4e49\u4ea7\u751f\u56f0\u60d1\u3002\u6b64\u5916\u5728\u9605\u8bfb\u5e93\u7684\u5ba2\u6237\u7aef\u4ee3\u7801\u65f6\uff0c\u56e0\u7f3a\u7701\u51fd\u6570\u53c2\u6570\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u8d39\u89e3\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u5982\u679c\u4f60\u60f3\u91cd\u8f7d\u4e00\u4e2a\u51fd\u6570\uff0c\u8003\u8651\u8ba9\u51fd\u6570\u540d\u5305\u542b\u53c2\u6570\u4fe1\u606f\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528AppendString()\u3001AppendInt()\u800c\u4e0d\u662fAppend()\u3002<\/p>\n<h2>3. \u7f3a\u7701\u53c2\u6570\uff08Default Arguments\uff09<\/h2>\n<p>\u7981\u6b62\u4f7f\u7528\u7f3a\u7701\u51fd\u6570\u53c2\u6570\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u7ecf\u5e38\u7528\u5230\u4e00\u4e2a\u51fd\u6570\u5e26\u6709\u5927\u91cf\u7f3a\u7701\u503c\uff0c\u5076\u5c14\u4f1a\u91cd\u5199\u4e00\u4e0b\u8fd9\u4e9b\u503c\uff0c\u7f3a\u7701\u53c2\u6570\u4e3a\u5f88\u5c11\u6d89\u53ca\u7684\u4f8b\u5916\u60c5\u51b5\u63d0\u4f9b\u4e86\u5c11\u5b9a\u4e49\u4e00\u4e9b\u51fd\u6570\u7684\u65b9\u4fbf\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u5927\u5bb6\u7ecf\u5e38\u4f1a\u901a\u8fc7\u67e5\u770b\u73b0\u6709\u4ee3\u7801\u786e\u5b9a\u5982\u4f55\u4f7f\u7528API\uff0c\u7f3a\u7701\u53c2\u6570\u4f7f\u5f97\u590d\u5236\u7c98\u8d34\u4ee5\u524d\u7684\u4ee3\u7801\u96be\u4ee5\u5448\u73b0\u6240\u6709\u53c2\u6570\uff0c\u5f53\u7f3a\u7701\u53c2\u6570\u4e0d\u9002\u7528\u4e8e\u65b0\u4ee3\u7801\u65f6\u53ef\u80fd\u5bfc\u81f4\u91cd\u5927\u95ee\u9898\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u6240\u6709\u53c2\u6570\u5fc5\u987b\u660e\u786e\u6307\u5b9a\uff0c\u5f3a\u5236\u7a0b\u5e8f\u5458\u8003\u8651API\u548c\u4f20\u5165\u7684\u5404\u53c2\u6570\u503c\uff0c\u907f\u514d\u4f7f\u7528\u53ef\u80fd\u4e0d\u4e3a\u7a0b\u5e8f\u5458\u6240\u77e5\u7684\u7f3a\u7701\u53c2\u6570\u3002<\/p>\n<h2>4. \u53d8\u957f\u6570\u7ec4\u548calloca\uff08Variable-Length Arrays and alloca()\uff09<\/h2>\n<p>\u7981\u6b62\u4f7f\u7528\u53d8\u957f\u6570\u7ec4\u548calloca()\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u53d8\u957f\u6570\u7ec4\u5177\u6709\u6d51\u7136\u5929\u6210\u7684\u8bed\u6cd5\uff0c\u53d8\u957f\u6570\u7ec4\u548calloca()\u4e5f\u90fd\u5f88\u9ad8\u6548\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u53d8\u957f\u6570\u7ec4\u548calloca()\u4e0d\u662f\u6807\u51c6C++\u7684\u7ec4\u6210\u90e8\u5206\uff0c\u66f4\u91cd\u8981\u7684\u662f\uff0c\u5b83\u4eec\u5728\u5806\u6808\uff08stack\uff09\u4e0a\u6839\u636e\u6570\u636e\u5206\u914d\u5927\u5c0f\u53ef\u80fd\u5bfc\u81f4\u96be\u4ee5\u53d1\u73b0\u7684\u5185\u5b58\u6cc4\u6f0f\uff1a\u201c\u5728\u6211\u7684\u673a\u5668\u4e0a\u8fd0\u884c\u7684\u597d\u597d\u7684\uff0c\u5230\u4e86\u4ea7\u54c1\u4e2d\u5374\u83ab\u540d\u5176\u5999\u7684\u6302\u6389\u4e86\u201d\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u4f7f\u7528\u5b89\u5168\u7684\u5206\u914d\u5668\uff08allocator\uff09\uff0c\u5982scoped_ptr\/scoped_array\u3002<\/p>\n<h2>5. \u53cb\u5143\uff08Friends\uff09<\/h2>\n<p>\u5141\u8bb8\u5408\u7406\u4f7f\u7528\u53cb\u5143\u7c7b\u53ca\u53cb\u5143\u51fd\u6570\u3002<\/p>\n<p>\u901a\u5e38\u5c06\u53cb\u5143\u5b9a\u4e49\u5728\u540c\u4e00\u6587\u4ef6\u4e0b\uff0c\u907f\u514d\u8bfb\u8005\u8dd1\u5230\u5176\u4ed6\u6587\u4ef6\u4e2d\u67e5\u627e\u5176\u5bf9\u67d0\u4e2a\u7c7b\u79c1\u6709\u6210\u5458\u7684\u4f7f\u7528\u3002\u7ecf\u5e38\u7528\u5230\u53cb\u5143\u7684\u4e00\u4e2a\u5730\u65b9\u662f\u5c06FooBuilder\u58f0\u660e\u4e3aFoo\u7684\u53cb\u5143\uff0cFooBuilder\u4ee5\u4fbf\u53ef\u4ee5\u6b63\u786e\u6784\u9020Foo\u7684\u5185\u90e8\u72b6\u6001\uff0c\u800c\u65e0\u9700\u5c06\u8be5\u72b6\u6001\u66b4\u9732\u51fa\u6765\u3002\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c06\u4e00\u4e2a\u5355\u5143\u6d4b\u8bd5\u7528\u7c7b\u58f0\u660e\u4e3a\u5f85\u6d4b\u7c7b\u7684\u53cb\u5143\u4f1a\u5f88\u65b9\u4fbf\u3002<\/p>\n<p>\u53cb\u5143\u5ef6\u4f38\u4e86\uff08\u4f46\u6ca1\u6709\u6253\u7834\uff09\u7c7b\u7684\u5c01\u88c5\u754c\u7ebf\uff0c\u5f53\u4f60\u5e0c\u671b\u53ea\u5141\u8bb8\u53e6\u4e00\u4e2a\u7c7b\u8bbf\u95ee\u67d0\u4e2a\u6210\u5458\u65f6\uff0c\u4f7f\u7528\u53cb\u5143\u901a\u5e38\u6bd4\u5c06\u5176\u58f0\u660e\u4e3apublic\u8981\u597d\u5f97\u591a\u3002\u5f53\u7136\uff0c\u5927\u591a\u6570\u7c7b\u5e94\u8be5\u53ea\u63d0\u4f9b\u516c\u5171\u6210\u5458\u4e0e\u5176\u4ea4\u4e92\u3002<\/p>\n<h2>6. \u5f02\u5e38\uff08Exceptions\uff09<\/h2>\n<p>\u4e0d\u8981\u4f7f\u7528C++\u5f02\u5e38\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<p>1) \u5f02\u5e38\u5141\u8bb8\u4e0a\u5c42\u5e94\u7528\u51b3\u5b9a\u5982\u4f55\u5904\u7406\u5728\u5e95\u5c42\u5d4c\u5957\u51fd\u6570\u4e2d\u53d1\u751f\u7684\u201c\u4e0d\u53ef\u80fd\u53d1\u751f\u201d\u7684\u5931\u8d25\uff0c\u4e0d\u50cf\u51fa\u9519\u4ee3\u7801\u7684\u8bb0\u5f55\u90a3\u4e48\u6a21\u7cca\u8d39\u89e3\uff1b<\/p>\n<p>2) \u5e94\u7528\u4e8e\u5176\u4ed6\u5f88\u591a\u73b0\u4ee3\u8bed\u8a00\u4e2d\uff0c\u5f15\u5165\u5f02\u5e38\u4f7f\u5f97C++\u4e0ePython\u3001Java\u53ca\u5176\u4ed6\u4e0eC++\u76f8\u8fd1\u7684\u8bed\u8a00\u66f4\u52a0\u517c\u5bb9\uff1b<\/p>\n<p>3) \u8bb8\u591aC++\u7b2c\u4e09\u65b9\u5e93\u4f7f\u7528\u5f02\u5e38\uff0c\u5173\u95ed\u5f02\u5e38\u5c06\u5bfc\u81f4\u96be\u4ee5\u4e0e\u4e4b\u7ed3\u5408\uff1b<\/p>\n<p>4) \u5f02\u5e38\u662f\u89e3\u51b3\u6784\u9020\u51fd\u6570\u5931\u8d25\u7684\u552f\u4e00\u65b9\u6848\uff0c\u867d\u7136\u53ef\u4ee5\u901a\u8fc7\u5de5\u5382\u51fd\u6570\uff08factory function\uff09\u6216Init()\u65b9\u6cd5\u6a21\u62df\u5f02\u5e38\uff0c\u4f46\u4ed6\u4eec\u5206\u522b\u9700\u8981\u5806\u5206\u914d\u6216\u65b0\u7684\u201c\u975e\u6cd5\u201d\u72b6\u6001\uff1b<\/p>\n<p>5) \u5728\u6d4b\u8bd5\u6846\u67b6\uff08testing framework\uff09\u4e2d\uff0c\u5f02\u5e38\u786e\u5b9e\u5f88\u597d\u7528\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong><\/p>\n<p>1) \u5728\u73b0\u6709\u51fd\u6570\u4e2d\u6dfb\u52a0throw\u8bed\u53e5\u65f6\uff0c\u5fc5\u987b\u68c0\u67e5\u6240\u6709\u8c03\u7528\u5904\uff0c\u5373\u4f7f\u5b83\u4eec\u81f3\u5c11\u5177\u6709\u57fa\u672c\u7684\u5f02\u5e38\u5b89\u5168\u4fdd\u62a4\uff0c\u6216\u8005\u7a0b\u5e8f\u6b63\u5e38\u7ed3\u675f\uff0c\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6355\u83b7\u8be5\u5f02\u5e38\u3002\u4f8b\u5982\uff1aif f() calls g() calls h()\uff0ch\u629b\u51fa\u88abf\u6355\u83b7\u7684\u5f02\u5e38\uff0cg\u5c31\u8981\u5f53\u5fc3\u4e86\uff0c\u907f\u514d\u6ca1\u6709\u5b8c\u5168\u6e05\u7406\uff1b<\/p>\n<p>2) \u901a\u4fd7\u4e00\u70b9\u8bf4\uff0c\u5f02\u5e38\u4f1a\u5bfc\u81f4\u7a0b\u5e8f\u63a7\u5236\u6d41\uff08control flow\uff09\u901a\u8fc7\u67e5\u770b\u4ee3\u7801\u65e0\u6cd5\u786e\u5b9a\uff1a\u51fd\u6570\u6709\u53ef\u80fd\u5728\u4e0d\u786e\u5b9a\u7684\u5730\u65b9\u8fd4\u56de\uff0c\u4ece\u800c\u5bfc\u81f4\u4ee3\u7801\u7ba1\u7406\u548c\u8c03\u8bd5\u56f0\u96be\uff0c\u5f53\u7136\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u89c4\u5b9a\u4f55\u65f6\u4f55\u5730\u5982\u4f55\u4f7f\u7528\u5f02\u5e38\u6765\u6700\u5c0f\u5316\u7684\u964d\u4f4e\u5f00\u9500\uff0c\u5374\u7ed9\u5f00\u53d1\u4eba\u5458\u5e26\u6765\u638c\u63e1\u8fd9\u4e9b\u89c4\u5b9a\u7684\u8d1f\u62c5\uff1b<\/p>\n<p>3) \u5f02\u5e38\u5b89\u5168\u9700\u8981RAII\u548c\u4e0d\u540c\u7f16\u7801\u5b9e\u8df5\u3002\u8f7b\u677e\u3001\u6b63\u786e\u7f16\u5199\u5f02\u5e38\u5b89\u5168\u4ee3\u7801\u9700\u8981\u5927\u91cf\u652f\u6491\u3002\u5141\u8bb8\u4f7f\u7528\u5f02\u5e38\uff1b<\/p>\n<p>4) \u52a0\u5165\u5f02\u5e38\u4f7f\u4e8c\u8fdb\u5236\u6267\u884c\u4ee3\u7801\u4f53\u79ef\u53d8\u5927\uff0c\u589e\u52a0\u4e86\u7f16\u8bd1\u65f6\u957f\uff08\u6216\u8bb8\u5f71\u54cd\u4e0d\u5927\uff09\uff0c\u8fd8\u53ef\u80fd\u589e\u52a0\u5730\u5740\u7a7a\u95f4\u538b\u529b\uff1b<\/p>\n<p>5) \u5f02\u5e38\u7684\u5b9e\u7528\u6027\u53ef\u80fd\u4f1a\u523a\u6fc0\u5f00\u53d1\u4eba\u5458\u5728\u4e0d\u6070\u5f53\u7684\u65f6\u5019\u629b\u51fa\u5f02\u5e38\uff0c\u6216\u8005\u5728\u4e0d\u5b89\u5168\u7684\u5730\u65b9\u4ece\u5f02\u5e38\u4e2d\u6062\u590d\uff0c\u4f8b\u5982\uff0c\u975e\u6cd5\u7528\u6237\u8f93\u5165\u53ef\u80fd\u5bfc\u81f4\u629b\u51fa\u5f02\u5e38\u3002\u5982\u679c\u5141\u8bb8\u4f7f\u7528\u5f02\u5e38\u4f1a\u4f7f\u5f97\u8fd9\u6837\u4e00\u7bc7\u7f16\u7a0b\u98ce\u683c\u6307\u5357\u957f\u51fa\u5f88\u591a\uff08\u8bd1\u8005\u6ce8\uff0c\u8fd9\u4e2a\u7406\u7531\u6709\u70b9\u7275\u5f3a:-(\uff09\uff01<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u4ece\u8868\u9762\u4e0a\u770b\uff0c\u4f7f\u7528\u5f02\u5e38\u5229\u5927\u4e8e\u5f0a\uff0c\u5c24\u5176\u662f\u5728\u65b0\u9879\u76ee\u4e2d\uff0c\u7136\u800c\uff0c\u5bf9\u4e8e\u73b0\u6709\u4ee3\u7801\uff0c\u5f15\u5165\u5f02\u5e38\u4f1a\u7275\u8fde\u5230\u6240\u6709\u4f9d\u8d56\u4ee3\u7801\u3002\u5982\u679c\u5141\u8bb8\u5f02\u5e38\u5728\u65b0\u9879\u76ee\u4e2d\u4f7f\u7528\uff0c\u5728\u8ddf\u4ee5\u524d\u6ca1\u6709\u4f7f\u7528\u5f02\u5e38\u7684\u4ee3\u7801\u6574\u5408\u65f6\u4e5f\u662f\u4e00\u4e2a\u9ebb\u70e6\u3002\u56e0\u4e3aGoogle\u73b0\u6709\u7684\u5927\u591a\u6570C++\u4ee3\u7801\u90fd\u6ca1\u6709\u5f02\u5e38\u5904\u7406\uff0c\u5f15\u5165\u5e26\u6709\u5f02\u5e38\u5904\u7406\u7684\u65b0\u4ee3\u7801\u76f8\u5f53\u56f0\u96be\u3002<\/p>\n<p>\u9274\u4e8eGoogle\u73b0\u6709\u4ee3\u7801\u4e0d\u63a5\u53d7\u5f02\u5e38\uff0c\u5728\u73b0\u6709\u4ee3\u7801\u4e2d\u4f7f\u7528\u5f02\u5e38\u6bd4\u5728\u65b0\u9879\u76ee\u4e2d\u4f7f\u7528\u7684\u4ee3\u4ef7\u591a\u5c11\u8981\u5927\u4e00\u70b9\uff0c\u8fc1\u79fb\u8fc7\u7a0b\u4f1a\u6bd4\u8f83\u6162\uff0c\u4e5f\u5bb9\u6613\u51fa\u9519\u3002\u6211\u4eec\u4e5f\u4e0d\u76f8\u4fe1\u5f02\u5e38\u7684\u6709\u6548\u66ff\u4ee3\u65b9\u6848\uff0c\u5982\u9519\u8bef\u4ee3\u7801\u3001\u65ad\u8a00\u7b49\uff0c\u90fd\u662f\u4e25\u91cd\u8d1f\u62c5\u3002<\/p>\n<p>\u6211\u4eec\u5e76\u4e0d\u662f\u57fa\u4e8e\u54f2\u5b66\u6216\u9053\u5fb7\u5c42\u9762\u53cd\u5bf9\u4f7f\u7528\u5f02\u5e38\uff0c\u800c\u662f\u5728\u5b9e\u8df5\u7684\u57fa\u7840\u4e0a\u3002\u56e0\u4e3a\u6211\u4eec\u5e0c\u671b\u4f7f\u7528Google\u4e0a\u7684\u5f00\u6e90\u9879\u76ee\uff0c\u4f46\u9879\u76ee\u4e2d\u4f7f\u7528\u5f02\u5e38\u4f1a\u4e3a\u6b64\u5e26\u6765\u4e0d\u4fbf\uff0c\u56e0\u4e3a\u6211\u4eec\u4e5f\u5efa\u8bae\u4e0d\u8981\u5728Google\u4e0a\u7684\u5f00\u6e90\u9879\u76ee\u4e2d\u4f7f\u7528\u5f02\u5e38\uff0c\u5982\u679c\u6211\u4eec\u9700\u8981\u628a\u8fd9\u4e9b\u9879\u76ee\u63a8\u5012\u91cd\u6765\u663e\u7136\u4e0d\u592a\u73b0\u5b9e\u3002<\/p>\n<p>\u5bf9\u4e8eWindows\u4ee3\u7801\u6765\u8bf4\uff0c\u8fd9\u4e00\u70b9\u6709\u4e2a\u4f8b\u5916\uff08\u7b49\u5230\u6700\u540e\u4e00\u7bc7\u5427:D\uff09\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u5bf9\u4e8e\u5f02\u5e38\u5904\u7406\uff0c\u663e\u7136\u4e0d\u662f\u77ed\u77ed\u51e0\u53e5\u8bdd\u80fd\u591f\u8bf4\u6e05\u695a\u7684\uff0c\u4ee5\u6784\u9020\u51fd\u6570\u4e3a\u4f8b\uff0c\u5f88\u591a<\/strong><strong>C++<\/strong><strong>\u4e66\u7c4d\u4e0a\u90fd\u63d0\u5230\u5f53\u6784\u9020\u5931\u8d25\u65f6\u53ea\u6709\u5f02\u5e38\u53ef\u4ee5\u5904\u7406\uff0c<\/strong><strong>Google<\/strong><strong>\u7981\u6b62\u4f7f\u7528\u5f02\u5e38\u8fd9\u4e00\u70b9\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u81ea\u8eab\u7684\u65b9\u4fbf\uff0c\u8bf4\u5927\u4e86\uff0c\u65e0\u975e\u662f\u57fa\u4e8e\u8f6f\u4ef6\u7ba1\u7406\u6210\u672c\u4e0a\uff0c\u5b9e\u9645\u4f7f\u7528\u4e2d\u8fd8\u662f\u81ea\u5df1\u51b3\u5b9a\u3002<\/strong><\/p>\n<h2>7. \u8fd0\u884c\u65f6\u7c7b\u578b\u8bc6\u522b\uff08Run-Time Type Information, RTTI\uff09<\/h2>\n<p>\u6211\u4eec\u7981\u6b62\u4f7f\u7528RTTI\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>RTTI\u5141\u8bb8\u7a0b\u5e8f\u5458\u5728\u8fd0\u884c\u65f6\u8bc6\u522bC++\u7c7b\u5bf9\u8c61\u7684\u7c7b\u578b\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong><\/p>\n<p>RTTI\u5728\u67d0\u4e9b\u5355\u5143\u6d4b\u8bd5\u4e2d\u975e\u5e38\u6709\u7528\uff0c\u5982\u5728\u8fdb\u884c\u5de5\u5382\u7c7b\u6d4b\u8bd5\u65f6\u7528\u4e8e\u68c0\u9a8c\u4e00\u4e2a\u65b0\u5efa\u5bf9\u8c61\u662f\u5426\u4e3a\u671f\u671b\u7684\u52a8\u6001\u7c7b\u578b\u3002<\/p>\n<p>\u9664\u6d4b\u8bd5\u5916\uff0c\u6781\u5c11\u7528\u5230\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u8fd0\u884c\u65f6\u8bc6\u522b\u7c7b\u578b\u610f\u5473\u8457\u8bbe\u8ba1\u672c\u8eab\u6709\u95ee\u9898\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u8fd0\u884c\u671f\u95f4\u786e\u5b9a\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fd9\u901a\u5e38\u8bf4\u660e\u4f60\u9700\u8981\u91cd\u65b0\u8003\u8651\u4f60\u7684\u7c7b\u7684\u8bbe\u8ba1\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u9664\u5355\u5143\u6d4b\u8bd5\u5916\uff0c\u4e0d\u8981\u4f7f\u7528RTTI\uff0c\u5982\u679c\u4f60\u53d1\u73b0\u9700\u8981\u6240\u5199\u4ee3\u7801\u56e0\u5bf9\u8c61\u7c7b\u578b\u4e0d\u540c\u800c\u52a8\u4f5c\u5404\u5f02\u7684\u8bdd\uff0c\u8003\u8651\u6362\u4e00\u79cd\u65b9\u5f0f\u8bc6\u522b\u5bf9\u8c61\u7c7b\u578b\u3002<\/p>\n<p>\u865a\u51fd\u6570\u53ef\u4ee5\u5b9e\u73b0\u968f\u5b50\u7c7b\u7c7b\u578b\u4e0d\u540c\u800c\u6267\u884c\u4e0d\u540c\u4ee3\u7801\uff0c\u5de5\u4f5c\u90fd\u662f\u4ea4\u7ed9\u5bf9\u8c61\u672c\u8eab\u53bb\u5b8c\u6210\u3002<\/p>\n<p>\u5982\u679c\u5de5\u4f5c\u5728\u5bf9\u8c61\u4e4b\u5916\u7684\u4ee3\u7801\u4e2d\u5b8c\u6210\uff0c\u8003\u8651\u53cc\u91cd\u5206\u53d1\u65b9\u6848\uff0c\u5982Visitor\u6a21\u5f0f\uff0c\u53ef\u4ee5\u65b9\u4fbf\u7684\u5728\u5bf9\u8c61\u672c\u8eab\u4e4b\u5916\u786e\u5b9a\u7c7b\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u5982\u679c\u4f60\u8ba4\u4e3a\u4e0a\u9762\u7684\u65b9\u6cd5\u4f60\u638c\u63e1\u4e0d\u4e86\uff0c\u53ef\u4ee5\u4f7f\u7528RTTI\uff0c\u4f46\u52a1\u5fc5\u8bf7\u4e09\u601d\uff0c\u4e0d\u8981\u53bb\u624b\u5de5\u5b9e\u73b0\u4e00\u4e2a\u8c8c\u4f3cRTTI\u7684\u65b9\u6848\uff08RTTI-like workaround\uff09\uff0c\u6211\u4eec\u53cd\u5bf9\u4f7f\u7528RTTI\uff0c\u540c\u6837\u53cd\u5bf9\u8d34\u4e0a\u7c7b\u578b\u6807\u7b7e\u7684\u8c8c\u4f3c\u7c7b\u7ee7\u627f\u7684\u66ff\u4ee3\u65b9\u6848\uff08\u8bd1\u8005\u6ce8\uff0c\u4f7f\u7528\u5c31\u4f7f\u7528\u5427\uff0c\u4e0d\u4f7f\u7528\u4e5f\u4e0d\u8981\u9020\u8f6e\u5b50:D\uff09\u3002<\/p>\n<h2>8. \u7c7b\u578b\u8f6c\u6362\uff08Casting\uff09<\/h2>\n<p>\u4f7f\u7528static_cast&lt;&gt;()\u7b49C++\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u4e0d\u8981\u4f7f\u7528int y = (int)x\u6216int y = int(x);\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>C++\u5f15\u5165\u4e86\u6709\u522b\u4e8eC\u7684\u4e0d\u540c\u7c7b\u578b\u7684\u7c7b\u578b\u8f6c\u6362\u64cd\u4f5c\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>C\u8bed\u8a00\u7684\u7c7b\u578b\u8f6c\u6362\u95ee\u9898\u5728\u4e8e\u64cd\u4f5c\u6bd4\u8f83\u542b\u7cca\uff1a\u6709\u65f6\u662f\u5728\u505a\u5f3a\u5236\u8f6c\u6362\uff08\u5982(int)3.5\uff09\uff0c\u6709\u65f6\u662f\u5728\u505a\u7c7b\u578b\u8f6c\u6362\uff08\u5982(int)&#8221;hello&#8221;\uff09\u3002\u53e6\u5916\uff0cC++\u7684\u7c7b\u578b\u8f6c\u6362\u67e5\u627e\u66f4\u5bb9\u6613\u3001\u66f4\u9192\u76ee\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u8bed\u6cd5\u6bd4\u8f83\u6076\u5fc3\uff08nasty\uff09\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong>\u4f7f\u7528C++\u98ce\u683c\u800c\u4e0d\u8981\u4f7f\u7528C\u98ce\u683c\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n<p>1) static_cast\uff1a\u548cC\u98ce\u683c\u8f6c\u6362\u76f8\u4f3c\u53ef\u505a\u503c\u7684\u5f3a\u5236\u8f6c\u6362\uff0c\u6216\u6307\u9488\u7684\u7236\u7c7b\u5230\u5b50\u7c7b\u7684\u660e\u786e\u7684\u5411\u4e0a\u8f6c\u6362\uff1b<\/p>\n<p>2) const_cast\uff1a\u79fb\u9664const\u5c5e\u6027\uff1b<\/p>\n<p>3) reinterpret_cast\uff1a\u6307\u9488\u7c7b\u578b\u548c\u6574\u578b\u6216\u5176\u4ed6\u6307\u9488\u95f4\u4e0d\u5b89\u5168\u7684\u76f8\u4e92\u8f6c\u6362\uff0c\u4ec5\u5728\u4f60\u5bf9\u6240\u505a\u4e00\u5207\u4e86\u7136\u4e8e\u5fc3\u65f6\u4f7f\u7528\uff1b<\/p>\n<p>4) dynamic_cast\uff1a\u9664\u6d4b\u8bd5\u5916\u4e0d\u8981\u4f7f\u7528\uff0c\u9664\u5355\u5143\u6d4b\u8bd5\u5916\uff0c\u5982\u679c\u4f60\u9700\u8981\u5728\u8fd0\u884c\u65f6\u786e\u5b9a\u7c7b\u578b\u4fe1\u606f\uff0c\u8bf4\u660e\u8bbe\u8ba1\u6709\u7f3a\u9677\uff08\u53c2\u8003<strong>RTTI<\/strong>\uff09\u3002<\/p>\n<h2>9. \u6d41\uff08Streams\uff09<\/h2>\n<p>\u53ea\u5728\u8bb0\u5f55\u65e5\u5fd7\u65f6\u4f7f\u7528\u6d41\u3002<\/p>\n<p><strong>\u5b9a\u4e49\uff1a<\/strong>\u6d41\u662fprintf()\u548cscanf()\u7684\u66ff\u4ee3\u3002<\/p>\n<p><strong>\u4f18\u70b9\uff1a<\/strong>\u6709\u4e86\u6d41\uff0c\u5728\u8f93\u51fa\u65f6\u4e0d\u9700\u8981\u5173\u5fc3\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u4e0d\u7528\u62c5\u5fc3\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u4e0e\u53c2\u6570\u5217\u8868\u4e0d\u5339\u914d\uff08\u867d\u7136\u5728gcc\u4e2d\u4f7f\u7528printf\u4e5f\u4e0d\u5b58\u5728\u8fd9\u4e2a\u95ee\u9898\uff09\uff0c\u6253\u5f00\u3001\u5173\u95ed\u5bf9\u5e94\u6587\u4ef6\u65f6\uff0c\u6d41\u53ef\u4ee5\u81ea\u52a8\u6784\u9020\u3001\u6790\u6784\u3002<\/p>\n<p><strong>\u7f3a\u70b9\uff1a<\/strong>\u6d41\u4f7f\u5f97pread()\u7b49\u529f\u80fd\u51fd\u6570\u5f88\u96be\u6267\u884c\uff0c\u5982\u679c\u4e0d\u4f7f\u7528printf\u4e4b\u7c7b\u7684\u51fd\u6570\u800c\u662f\u4f7f\u7528\u6d41\u5f88\u96be\u5bf9\u683c\u5f0f\u8fdb\u884c\u64cd\u4f5c\uff08\u5c24\u5176\u662f\u5e38\u7528\u7684\u683c\u5f0f\u5b57\u7b26\u4e32%.*s\uff09\uff0c\u6d41\u4e0d\u652f\u6301\u5b57\u7b26\u4e32\u64cd\u4f5c\u7b26\u91cd\u65b0\u5b9a\u5e8f\uff08%1s\uff09\uff0c\u800c\u8fd9\u4e00\u70b9\u5bf9\u56fd\u9645\u5316\u5f88\u6709\u7528\u3002<\/p>\n<p><strong>\u7ed3\u8bba\uff1a<\/strong><\/p>\n<p>\u4e0d\u8981\u4f7f\u7528\u6d41\uff0c\u9664\u975e\u662f\u65e5\u5fd7\u63a5\u53e3\u9700\u8981\uff0c\u4f7f\u7528printf\u4e4b\u7c7b\u7684\u4ee3\u66ff\u3002<\/p>\n<p>\u4f7f\u7528\u6d41\u8fd8\u6709\u5f88\u591a\u5229\u5f0a\uff0c\u4ee3\u7801\u4e00\u81f4\u6027\u80dc\u8fc7\u4e00\u5207\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u6d41\u3002<\/p>\n<p><strong>\u62d3\u5c55\u8ba8\u8bba\uff1a<\/strong><\/p>\n<p>\u5bf9\u8fd9\u4e00\u6761\u89c4\u5219\u5b58\u5728\u4e00\u4e9b\u4e89\u8bba\uff0c\u8fd9\u513f\u7ed9\u51fa\u6df1\u5c42\u6b21\u539f\u56e0\u3002\u56de\u5fc6\u552f\u4e00\u6027\u539f\u5219\uff08Only One Way\uff09\uff1a\u6211\u4eec\u5e0c\u671b\u5728\u4efb\u4f55\u65f6\u5019\u90fd\u53ea\u4f7f\u7528\u4e00\u79cd\u786e\u5b9a\u7684I\/O\u7c7b\u578b\uff0c\u4f7f\u4ee3\u7801\u5728\u6240\u6709I\/O\u5904\u4fdd\u6301\u4e00\u81f4\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u4e0d\u5e0c\u671b\u7528\u6237\u6765\u51b3\u5b9a\u662f\u4f7f\u7528\u6d41\u8fd8\u662fprintf + read\/write\uff0c\u6211\u4eec\u5e94\u8be5\u51b3\u5b9a\u5230\u5e95\u7528\u54ea\u4e00\u79cd\u65b9\u5f0f\u3002\u628a\u65e5\u5fd7\u4f5c\u4e3a\u4f8b\u5916\u662f\u56e0\u4e3a\u6d41\u975e\u5e38\u9002\u5408\u8fd9\u4e48\u505a\uff0c\u4e5f\u6709\u4e00\u5b9a\u7684\u5386\u53f2\u539f\u56e0\u3002<\/p>\n<p>\u6d41\u7684\u652f\u6301\u8005\u4eec\u4e3b\u5f20\u6d41\u662f\u4e0d\u4e8c\u4e4b\u9009\uff0c\u4f46\u89c2\u70b9\u5e76\u4e0d\u662f\u90a3\u4e48\u6e05\u6670\u6709\u529b\uff0c\u4ed6\u4eec\u6240\u6307\u51fa\u6d41\u7684\u6240\u6709\u4f18\u52bf\u4e5f\u6b63\u662f\u5176\u52a3\u52bf\u6240\u5728\u3002\u6d41\u6700\u5927\u7684\u4f18\u52bf\u662f\u5728\u8f93\u51fa\u65f6\u4e0d\u9700\u8981\u5173\u5fc3\u8f93\u51fa\u5bf9\u8c61\u7684\u7c7b\u578b\uff0c\u8fd9\u662f\u4e00\u4e2a\u4eae\u70b9\uff0c\u4e5f\u662f\u4e00\u4e2a\u4e0d\u8db3\uff1a\u5f88\u5bb9\u6613\u7528\u9519\u7c7b\u578b\uff0c\u800c\u7f16\u8bd1\u5668\u4e0d\u4f1a\u62a5\u8b66\u3002\u4f7f\u7528\u6d41\u65f6\u5bb9\u6613\u9020\u6210\u7684\u4e00\u7c7b\u9519\u8bef\u662f\uff1a<\/p>\n<p>cout &lt;&lt; this;\u00a0 \/\/ Prints the address<\/p>\n<p>cout &lt;&lt; *this;\u00a0 \/\/ Prints the contents<\/p>\n<p>\u7f16\u8bd1\u5668\u4e0d\u4f1a\u62a5\u9519\uff0c\u56e0\u4e3a&lt;&lt;\u88ab\u91cd\u8f7d\uff0c\u5c31\u56e0\u4e3a\u8fd9\u4e00\u70b9\u6211\u4eec\u53cd\u5bf9\u4f7f\u7528\u64cd\u4f5c\u7b26\u91cd\u8f7d\u3002<\/p>\n<p>\u6709\u4eba\u8bf4printf\u7684\u683c\u5f0f\u5316\u4e11\u964b\u4e0d\u582a\u3001\u6613\u8bfb\u6027\u5dee\uff0c\u4f46\u6d41\u4e5f\u597d\u4e0d\u5230\u54ea\u513f\u53bb\u3002\u770b\u770b\u4e0b\u9762\u4e24\u6bb5\u4ee3\u7801\u5427\uff0c\u54ea\u4e2a\u66f4\u52a0\u6613\u8bfb\uff1f<\/p>\n<p>cerr &lt;&lt; &#8220;Error connecting to &#8216;&#8221; &lt;&lt; foo-&gt;bar()-&gt;hostname.first<\/p>\n<p>&lt;&lt; &#8220;:&#8221; &lt;&lt; foo-&gt;bar()-&gt;hostname.second &lt;&lt; &#8220;: &#8221; &lt;&lt; strerror(errno);<\/p>\n<p>&nbsp;<\/p>\n<p>fprintf(stderr, &#8220;Error connecting to &#8216;%s:%u: %s&#8221;,<\/p>\n<p>foo-&gt;bar()-&gt;hostname.first, foo-&gt;bar()-&gt;hostname.second,<\/p>\n<p>strerror(errno));<\/p>\n<p>\u4f60\u53ef\u80fd\u4f1a\u8bf4\uff0c\u201c\u628a\u6d41\u5c01\u88c5\u4e00\u4e0b\u5c31\u4f1a\u6bd4\u8f83\u597d\u4e86\u201d\uff0c\u8fd9\u513f\u53ef\u4ee5\uff0c\u5176\u4ed6\u5730\u65b9\u5462\uff1f\u800c\u4e14\u4e0d\u8981\u5fd8\u4e86\uff0c\u6211\u4eec\u7684\u76ee\u6807\u662f\u4f7f\u8bed\u8a00\u5c3d\u53ef\u80fd\u5c0f\uff0c\u800c\u4e0d\u662f\u6dfb\u52a0\u4e00\u4e9b\u522b\u4eba\u9700\u8981\u5b66\u4e60\u7684\u65b0\u7684\u5185\u5bb9\u3002<\/p>\n<p>\u6bcf\u4e00\u79cd\u65b9\u5f0f\u90fd\u662f\u5404\u6709\u5229\u5f0a\uff0c\u201c\u6ca1\u6709\u6700\u597d\uff0c\u53ea\u6709\u66f4\u597d\u201d\uff0c\u7b80\u5355\u5316\u7684\u6559\u6761\u544a\u8beb\u6211\u4eec\u5fc5\u987b\u4ece\u4e2d\u9009\u62e9\u5176\u4e00\uff0c\u6700\u540e\u7684\u591a\u6570\u51b3\u5b9a\u662fprintf + read\/write\u3002<\/p>\n<h2>10. \u524d\u7f6e\u81ea\u589e\u548c\u81ea\u51cf\uff08Preincrement and Predecrement\uff09<\/h2>\n<p>\u5bf9\u4e8e\u8fed\u4ee3\u5668\u548c\u5176\u4ed6\u6a21\u677f\u5bf9\u8c61\u4f7f\u7528\u524d\u7f00\u5f62\u5f0f\uff08++i\uff09\u7684\u81ea\u589e\u3001\u81ea\u51cf\u8fd0\u7b97\u7b26\u3002<\/p>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a\u5bf9\u4e8e\u53d8\u91cf\u5728\u81ea\u589e\uff08++i\u6216i++\uff09\u6216\u81ea\u51cf\uff08&#8211;i\u6216i&#8211;\uff09\u540e\u8868\u8fbe\u5f0f\u7684\u503c\u53c8\u6ca1\u6709\u6ca1\u7528\u5230\u7684\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u786e\u5b9a\u5230\u5e95\u662f\u4f7f\u7528\u524d\u7f6e\u8fd8\u662f\u540e\u7f6e\u7684\u81ea\u589e\u81ea\u51cf\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u4e0d\u8003\u8651\u8fd4\u56de\u503c\u7684\u8bdd\uff0c\u524d\u7f6e\u81ea\u589e\uff08++i\uff09\u901a\u5e38\u8981\u6bd4\u540e\u7f6e\u81ea\u589e\uff08i++\uff09\u6548\u7387\u66f4\u9ad8\uff0c\u56e0\u4e3a\u540e\u7f6e\u7684\u81ea\u589e\u81ea\u51cf\u9700\u8981\u5bf9\u8868\u8fbe\u5f0f\u7684\u503ci\u8fdb\u884c\u4e00\u6b21\u62f7\u8d1d\uff0c\u5982\u679ci\u662f\u8fed\u4ee3\u5668\u6216\u5176\u4ed6\u975e\u6570\u503c\u7c7b\u578b\uff0c\u62f7\u8d1d\u7684\u4ee3\u4ef7\u662f\u6bd4\u8f83\u5927\u7684\u3002\u65e2\u7136\u4e24\u79cd\u81ea\u589e\u65b9\u5f0f\u52a8\u4f5c\u4e00\u6837\uff08\u8bd1\u8005\u6ce8\uff0c\u4e0d\u8003\u8651\u8868\u8fbe\u5f0f\u7684\u503c\uff0c\u76f8\u4fe1\u4f60\u77e5\u9053\u6211\u5728\u8bf4\u4ec0\u4e48\uff09\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u76f4\u63a5\u4f7f\u7528\u524d\u7f6e\u81ea\u589e\u5462\uff1f<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1aC\u8bed\u8a00\u4e2d\uff0c\u5f53\u8868\u8fbe\u5f0f\u7684\u503c\u6ca1\u6709\u4f7f\u7528\u65f6\uff0c\u4f20\u7edf\u7684\u505a\u6cd5\u662f\u4f7f\u7528\u540e\u7f6e\u81ea\u589e\uff0c\u7279\u522b\u662f\u5728for\u5faa\u73af\u4e2d\uff0c\u6709\u4e9b\u4eba\u89c9\u5f97\u540e\u7f6e\u81ea\u589e\u66f4\u52a0\u6613\u61c2\uff0c\u56e0\u4e3a\u8fd9\u5f88\u50cf\u81ea\u7136\u8bed\u8a00\uff0c\u4e3b\u8bed\uff08i\uff09\u5728\u8c13\u8bed\u52a8\u8bcd\uff08++\uff09\u524d\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a\u5bf9\u7b80\u5355\u6570\u503c\uff08\u975e\u5bf9\u8c61\uff09\u6765\u8bf4\uff0c\u4e24\u79cd\u90fd\u65e0\u6240\u8c13\uff0c\u5bf9\u8fed\u4ee3\u5668\u548c\u6a21\u677f\u7c7b\u578b\u6765\u8bf4\uff0c\u8981\u4f7f\u7528\u524d\u7f6e\u81ea\u589e\uff08\u81ea\u51cf\uff09\u3002<\/p>\n<h2>11. const\u7684\u4f7f\u7528\uff08Use of const\uff09<\/h2>\n<p>\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u4f60\u5728\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\u90fd\u8981\u4f7f\u7528const\u3002<\/p>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1a\u5728\u58f0\u660e\u7684\u53d8\u91cf\u6216\u53c2\u6570\u524d\u52a0\u4e0a\u5173\u952e\u5b57const\u7528\u4e8e\u6307\u660e\u53d8\u91cf\u503c\u4e0d\u53ef\u4fee\u6539\uff08\u5982const int foo\uff09\uff0c\u4e3a\u7c7b\u4e2d\u7684\u51fd\u6570\u52a0\u4e0aconst\u9650\u5b9a\u8868\u660e\u8be5\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u7c7b\u6210\u5458\u53d8\u91cf\u7684\u72b6\u6001\uff08\u5982class Foo { int Bar(char c) const; };\uff09\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u4eba\u4eec\u66f4\u5bb9\u6613\u7406\u89e3\u53d8\u91cf\u662f\u5982\u4f55\u4f7f\u7528\u7684\uff0c\u7f16\u8f91\u5668\u53ef\u4ee5\u66f4\u597d\u5730\u8fdb\u884c\u7c7b\u578b\u68c0\u6d4b\u3001\u66f4\u597d\u5730\u751f\u6210\u4ee3\u7801\u3002\u4eba\u4eec\u5bf9\u7f16\u5199\u6b63\u786e\u7684\u4ee3\u7801\u66f4\u52a0\u81ea\u4fe1\uff0c\u56e0\u4e3a\u4ed6\u4eec\u77e5\u9053\u6240\u8c03\u7528\u7684\u51fd\u6570\u88ab\u9650\u5b9a\u4e86\u80fd\u6216\u4e0d\u80fd\u4fee\u6539\u53d8\u91cf\u503c\u3002\u5373\u4f7f\u662f\u5728\u65e0\u9501\u7684\u591a\u7ebf\u7a0b\u7f16\u7a0b\u4e2d\uff0c\u4eba\u4eec\u4e5f\u77e5\u9053\u4ec0\u4e48\u6837\u7684\u51fd\u6570\u662f\u5b89\u5168\u7684\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u5982\u679c\u4f60\u5411\u4e00\u4e2a\u51fd\u6570\u4f20\u5165const\u53d8\u91cf\uff0c\u51fd\u6570\u539f\u578b\u4e2d\u4e5f\u5fc5\u987b\u662fconst\u7684\uff08\u5426\u5219\u53d8\u91cf\u9700\u8981const_cast\u7c7b\u578b\u8f6c\u6362\uff09\uff0c\u5728\u8c03\u7528\u5e93\u51fd\u6570\u65f6\u8fd9\u5c24\u5176\u662f\u4e2a\u9ebb\u70e6\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1aconst\u53d8\u91cf\u3001\u6570\u636e\u6210\u5458\u3001\u51fd\u6570\u548c\u53c2\u6570\u4e3a\u7f16\u8bd1\u65f6\u7c7b\u578b\u68c0\u6d4b\u589e\u52a0\u4e86\u4e00\u5c42\u4fdd\u969c\uff0c\u66f4\u597d\u7684\u5c3d\u65e9\u53d1\u73b0\u9519\u8bef\u3002\u56e0\u6b64\uff0c\u6211\u4eec\u5f3a\u70c8\u5efa\u8bae\u5728\u4efb\u4f55\u53ef\u4ee5\u4f7f\u7528\u7684\u60c5\u51b5\u4e0b\u4f7f\u7528const\uff1a<\/p>\n<p>1) \u5982\u679c\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4f20\u5165\u7684\u5f15\u7528\u6216\u6307\u9488\u7c7b\u578b\u7684\u53c2\u6570\uff0c\u8fd9\u6837\u7684\u53c2\u6570\u5e94\u8be5\u4e3aconst\uff1b<\/p>\n<p>2) \u5c3d\u53ef\u80fd\u5c06\u51fd\u6570\u58f0\u660e\u4e3aconst\uff0c\u8bbf\u95ee\u51fd\u6570\u5e94\u8be5\u603b\u662fconst\uff0c\u5176\u4ed6\u51fd\u6570\u5982\u679c\u4e0d\u4f1a\u4fee\u6539\u4efb\u4f55\u6570\u636e\u6210\u5458\u4e5f\u5e94\u8be5\u662fconst\uff0c\u4e0d\u8981\u8c03\u7528\u975econst\u51fd\u6570\uff0c\u4e0d\u8981\u8fd4\u56de\u5bf9\u6570\u636e\u6210\u5458\u7684\u975econst\u6307\u9488\u6216\u5f15\u7528\uff1b<\/p>\n<p>3) \u5982\u679c\u6570\u636e\u6210\u5458\u5728\u5bf9\u8c61\u6784\u9020\u4e4b\u540e\u4e0d\u518d\u6539\u53d8\uff0c\u53ef\u5c06\u5176\u5b9a\u4e49\u4e3aconst\u3002<\/p>\n<p>\u7136\u800c\uff0c\u4e5f<strong>\u4e0d\u8981\u5bf9<\/strong><strong>const<\/strong><strong>\u8fc7\u5ea6\u4f7f\u7528<\/strong>\uff0c\u50cfconst int * const * const x;\u5c31\u6709\u4e9b\u8fc7\u4e86\uff0c\u5373\u4fbf\u8fd9\u6837\u5199\u7cbe\u786e\u63cf\u8ff0\u4e86x\uff0c\u5176\u5b9e\u5199\u6210const int** x\u5c31\u53ef\u4ee5\u4e86\u3002<\/p>\n<p>\u5173\u952e\u5b57mutable\u53ef\u4ee5\u4f7f\u7528\uff0c\u4f46\u662f\u5728\u591a\u7ebf\u7a0b\u4e2d\u662f\u4e0d\u5b89\u5168\u7684\uff0c\u4f7f\u7528\u65f6\u9996\u5148\u8981\u8003\u8651\u7ebf\u7a0b\u5b89\u5168\u3002<\/p>\n<p><strong>const<\/strong><strong>\u4f4d\u7f6e<\/strong>\uff1a<\/p>\n<p>\u6709\u4eba\u559c\u6b22int const *foo\u5f62\u5f0f\u4e0d\u559c\u6b22const int* foo\uff0c\u4ed6\u4eec\u8ba4\u4e3a\u524d\u8005\u66f4\u52a0\u4e00\u81f4\u56e0\u6b64\u53ef\u8bfb\u6027\u66f4\u597d\uff1a\u9075\u5faa\u4e86const\u603b\u4f4d\u4e8e\u5176\u63cf\u8ff0\u7684\u5bf9\u8c61\uff08int\uff09\u4e4b\u540e\u7684\u539f\u5219\u3002\u4f46\u662f\uff0c\u4e00\u81f4\u6027\u539f\u5219\u4e0d\u9002\u7528\u4e8e\u6b64\uff0c\u201c\u4e0d\u8981\u8fc7\u5ea6\u4f7f\u7528\u201d\u7684\u6743\u5a01\u62b5\u6d88\u4e86\u4e00\u81f4\u6027\u4f7f\u7528\u3002\u5c06const\u653e\u5728\u524d\u9762\u624d\u66f4\u6613\u8bfb\uff0c\u56e0\u4e3a\u5728\u81ea\u7136\u8bed\u8a00\u4e2d\u5f62\u5bb9\u8bcd\uff08const\uff09\u662f\u5728\u540d\u8bcd\uff08int\uff09\u4e4b\u524d\u7684\u3002<\/p>\n<p>\u8fd9\u662f\u8bf4\uff0c\u6211\u4eec\u63d0\u5021const\u5728\u524d\uff0c\u5e76\u4e0d\u662f\u8981\u6c42\uff0c\u4f46\u8981\u517c\u987e\u4ee3\u7801\u7684\u4e00\u81f4\u6027\uff01<\/p>\n<h2>12. \u6574\u578b\uff08Integer Types\uff09<\/h2>\n<p>C++\u5185\u5efa\u6574\u578b\u4e2d\uff0c\u552f\u4e00\u7528\u5230\u7684\u662fint\uff0c\u5982\u679c\u7a0b\u5e8f\u4e2d\u9700\u8981\u4e0d\u540c\u5927\u5c0f\u7684\u53d8\u91cf\uff0c\u53ef\u4ee5\u4f7f\u7528&lt;stdint.h&gt;\u4e2d\u7684\u7cbe\u786e\u5bbd\u5ea6\uff08precise-width\uff09\u7684\u6574\u578b\uff0c\u5982int16_t\u3002<\/p>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1aC++\u6ca1\u6709\u6307\u5b9a\u6574\u578b\u7684\u5927\u5c0f\uff0c\u901a\u5e38\u4eba\u4eec\u8ba4\u4e3ashort\u662f16\u4f4d\uff0cint\u662f32\u4f4d\uff0clong\u662f32\u4f4d\uff0clong long\u662f64\u4f4d\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u4fdd\u6301\u58f0\u660e\u7edf\u4e00\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1aC++\u4e2d\u6574\u578b\u5927\u5c0f\u56e0\u7f16\u8bd1\u5668\u548c\u4f53\u7cfb\u7ed3\u6784\u7684\u4e0d\u540c\u800c\u4e0d\u540c\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a<\/p>\n<p>&lt;stdint.h&gt;\u5b9a\u4e49\u4e86int16_t\u3001uint32_t\u3001int64_t\u7b49\u6574\u578b\uff0c\u5728\u9700\u8981\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\u65f6\u53ef\u4ee5\u4f7f\u7528\u5b83\u4eec\u4ee3\u66ffshort\u3001unsigned long long\u7b49\uff0c\u5728C\u6574\u578b\u4e2d\uff0c\u53ea\u4f7f\u7528int\u3002\u9002\u5f53\u60c5\u51b5\u4e0b\uff0c\u63a8\u8350\u4f7f\u7528\u6807\u51c6\u7c7b\u578b\u5982size_t\u548cptrdiff_t\u3002<\/p>\n<p>\u6700\u5e38\u4f7f\u7528\u7684\u662f\uff0c\u5bf9\u6574\u6570\u6765\u8bf4\uff0c\u901a\u5e38\u4e0d\u4f1a\u7528\u5230\u592a\u5927\uff0c\u5982\u5faa\u73af\u8ba1\u6570\u7b49\uff0c\u53ef\u4ee5\u4f7f\u7528\u666e\u901a\u7684int\u3002\u4f60\u53ef\u4ee5\u8ba4\u4e3aint\u81f3\u5c11\u4e3a32\u4f4d\uff0c\u4f46\u4e0d\u8981\u8ba4\u4e3a\u5b83\u4f1a\u591a\u4e8e32\u4f4d\uff0c\u9700\u898164\u4f4d\u6574\u578b\u7684\u8bdd\uff0c\u53ef\u4ee5\u4f7f\u7528int64_t\u6216uint64_t\u3002<\/p>\n<p>\u5bf9\u4e8e\u5927\u6574\u6570\uff0c\u4f7f\u7528int64_t\u3002<\/p>\n<p>\u4e0d\u8981\u4f7f\u7528uint32_t\u7b49\u65e0\u7b26\u53f7\u6574\u578b\uff0c\u9664\u975e\u4f60\u662f\u5728\u8868\u793a\u4e00\u4e2a\u4f4d\u7ec4\uff08bit pattern\uff09\u800c\u4e0d\u662f\u4e00\u4e2a\u6570\u503c\u3002\u5373\u4f7f\u6570\u503c\u4e0d\u4f1a\u4e3a\u8d1f\u503c\u4e5f\u4e0d\u8981\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\uff0c\u4f7f\u7528\u65ad\u8a00\uff08assertion\uff0c\u8bd1\u8005\u6ce8\uff0c\u8fd9\u4e00\u70b9\u5f88\u6709\u9053\u7406\uff0c\u8ba1\u7b97\u673a\u53ea\u4f1a\u6839\u636e\u53d8\u91cf\u3001\u8fd4\u56de\u503c\u7b49\u6709\u65e0\u7b26\u53f7\u786e\u5b9a\u6570\u503c\u6b63\u8d1f\uff0c\u4ecd\u7136\u65e0\u6cd5\u786e\u5b9a\u5bf9\u9519\uff09\u6765\u4fdd\u62a4\u6570\u636e\u3002<\/p>\n<p><strong>\u65e0\u7b26\u53f7\u6574\u578b<\/strong>\uff1a<\/p>\n<p>\u6709\u4e9b\u4eba\uff0c\u5305\u62ec\u4e00\u4e9b\u6559\u79d1\u4e66\u4f5c\u8005\uff0c\u63a8\u8350\u4f7f\u7528\u65e0\u7b26\u53f7\u7c7b\u578b\u8868\u793a\u975e\u8d1f\u6570\uff0c\u7c7b\u578b\u8868\u660e\u4e86\u6570\u503c\u53d6\u503c\u5f62\u5f0f\u3002\u4f46\u662f\uff0c\u5728C\u8bed\u8a00\u4e2d\uff0c\u8fd9\u4e00\u4f18\u70b9\u88ab\u7531\u5176\u5bfc\u81f4\u7684bugs\u6240\u6df9\u6ca1\u3002\u770b\u770b\uff1a<\/p>\n<p>for (unsigned int i = foo.Length()-1; i &gt;= 0; &#8211;i) &#8230;<\/p>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u6c38\u8fdc\u4e0d\u4f1a\u7ec8\u6b62\uff01\u6709\u65f6gcc\u4f1a\u53d1\u73b0\u8be5bug\u5e76\u62a5\u8b66\uff0c\u4f46\u901a\u5e38\u4e0d\u4f1a\u3002\u7c7b\u4f3c\u7684bug\u8fd8\u4f1a\u51fa\u73b0\u5728\u6bd4\u8f83\u6709\u7b26\u5408\u53d8\u91cf\u548c\u65e0\u7b26\u53f7\u53d8\u91cf\u65f6\uff0c\u4e3b\u8981\u662fC\u7684\u7c7b\u578b\u63d0\u5347\u673a\u5236\uff08type-promotion scheme\uff0cC\u8bed\u8a00\u4e2d\u5404\u79cd\u5185\u5efa\u7c7b\u578b\u4e4b\u95f4\u7684\u63d0\u5347\u8f6c\u6362\u5173\u7cfb\uff09\u4f1a\u81f4\u4f7f\u65e0\u7b26\u53f7\u7c7b\u578b\u7684\u884c\u4e3a\u51fa\u4e4e\u4f60\u7684\u610f\u6599\u3002<\/p>\n<p>\u56e0\u6b64\uff0c\u4f7f\u7528\u65ad\u8a00\u58f0\u660e\u53d8\u91cf\u4e3a\u975e\u8d1f\u6570\uff0c\u4e0d\u8981\u4f7f\u7528\u65e0\u7b26\u53f7\u578b\u3002<\/p>\n<h2>13. 64\u4f4d\u4e0b\u7684\u53ef\u79fb\u690d\u6027\uff0864-bit Portability\uff09<\/h2>\n<p>\u4ee3\u7801\u572864\u4f4d\u548c32\u4f4d\u7684\u7cfb\u7edf\u4e2d\uff0c\u539f\u5219\u4e0a\u5e94\u8be5\u90fd\u6bd4\u8f83\u53cb\u597d\uff0c\u5c24\u5176\u5bf9\u4e8e\u8f93\u51fa\u3001\u6bd4\u8f83\u3001\u7ed3\u6784\u5bf9\u9f50\uff08structure alignment\uff09\u6765\u8bf4\uff1a<\/p>\n<p>1) printf()\u6307\u5b9a\u7684\u4e00\u4e9b\u7c7b\u578b\u572832\u4f4d\u548c64\u4f4d\u7cfb\u7edf\u4e0a\u53ef\u79fb\u690d\u6027\u4e0d\u662f\u5f88\u597d\uff0cC99\u6807\u51c6\u5b9a\u4e49\u4e86\u4e00\u4e9b\u53ef\u79fb\u690d\u7684\u683c\u5f0f\u3002\u4e0d\u5e78\u7684\u662f\uff0cMSVC 7.1\u5e76\u975e\u5168\u90e8\u652f\u6301\uff0c\u800c\u4e14\u6807\u51c6\u4e2d\u4e5f\u6709\u6240\u9057\u6f0f\u3002\u6240\u4ee5\u6709\u65f6\u6211\u4eec\u5c31\u4e0d\u5f97\u4e0d\u81ea\u5df1\u5b9a\u4e49\u4e11\u964b\u7684\u7248\u672c\uff08\u4f7f\u7528\u6807\u51c6\u98ce\u683c\u8981\u5305\u542b\u6587\u4ef6inttypes.h\uff09\uff1a<\/p>\n<p>\/\/ printf macros for size_t, in the style of inttypes.h<\/p>\n<p>#ifdef _LP64<\/p>\n<p>#define __PRIS_PREFIX &#8220;z&#8221;<\/p>\n<p>#else<\/p>\n<p>#define __PRIS_PREFIX<\/p>\n<p>#endif<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Use these macros after a % in a printf format string<\/p>\n<p>\/\/ to get correct 32\/64 bit behavior, like this:<\/p>\n<p>\/\/ size_t size = records.size();<\/p>\n<p>\/\/ printf(&#8220;%&#8221;PRIuS&#8221;\\n&#8221;, size);<\/p>\n<p>&nbsp;<\/p>\n<p>#define PRIdS __PRIS_PREFIX &#8220;d&#8221;<\/p>\n<p>#define PRIxS __PRIS_PREFIX &#8220;x&#8221;<\/p>\n<p>#define PRIuS __PRIS_PREFIX &#8220;u&#8221;<\/p>\n<p>#define PRIXS __PRIS_PREFIX &#8220;X&#8221;<\/p>\n<p>#define PRIoS __PRIS_PREFIX &#8220;o&#8221;<\/p>\n<table width=\"529\">\n<tbody>\n<tr>\n<td width=\"154\">\u7c7b\u578b<\/td>\n<td width=\"109\">\u4e0d\u8981\u4f7f\u7528<\/td>\n<td width=\"142\">\u4f7f\u7528<\/td>\n<td width=\"122\">\u5907\u6ce8<\/td>\n<\/tr>\n<tr>\n<td width=\"154\">void *\uff08\u6216\u5176\u4ed6\u6307\u9488\u7c7b\u578b\uff09<\/td>\n<td width=\"109\">%lx<\/td>\n<td width=\"142\">%p<\/td>\n<td width=\"122\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td width=\"154\">int64_t<\/td>\n<td width=\"109\">%qd, %lld<\/td>\n<td width=\"142\">%&#8221;PRId64&#8243;<\/td>\n<td width=\"122\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td width=\"154\">uint64_t<\/td>\n<td width=\"109\">%qu, %llu, %llx<\/td>\n<td width=\"142\">%&#8221;PRIu64&#8243;, %&#8221;PRIx64&#8243;<\/td>\n<td width=\"122\">&nbsp;<\/td>\n<\/tr>\n<tr>\n<td width=\"154\">size_t<\/td>\n<td width=\"109\">%u<\/td>\n<td width=\"142\">%&#8221;PRIuS&#8221;, %&#8221;PRIxS&#8221;<\/td>\n<td width=\"122\">C99\u6307\u5b9a%zu<\/td>\n<\/tr>\n<tr>\n<td width=\"154\">ptrdiff_t<\/td>\n<td width=\"109\">%d<\/td>\n<td width=\"142\">%&#8221;PRIdS&#8221;<\/td>\n<td width=\"122\">C99\u6307\u5b9a%zd<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\u5b8fPRI*\u4f1a\u88ab\u7f16\u8bd1\u5668\u6269\u5c55\u4e3a\u72ec\u7acb\u5b57\u7b26\u4e32\uff0c\u56e0\u6b64\u5982\u679c\u4f7f\u7528\u975e\u5e38\u91cf\u7684\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u9700\u8981\u5c06\u5b8f\u7684\u503c\u800c\u4e0d\u662f\u5b8f\u540d\u63d2\u5165\u683c\u5f0f\u4e2d\uff0c\u5728\u4f7f\u7528\u5b8fPRI*\u65f6\u540c\u6837\u53ef\u4ee5\u5728%\u540e\u6307\u5b9a\u957f\u5ea6\u7b49\u4fe1\u606f\u3002\u4f8b\u5982\uff0cprintf(&#8220;x = %30&#8243;PRIuS&#8221;\\n&#8221;, x)\u572832\u4f4dLinux\u4e0a\u5c06\u88ab\u6269\u5c55\u4e3aprintf(&#8220;x = %30&#8221; &#8220;u&#8221; &#8220;\\n&#8221;, x)\uff0c\u7f16\u8bd1\u5668\u4f1a\u5904\u7406\u4e3aprintf(&#8220;x = %30u\\n&#8221;, x)\u3002<\/p>\n<p>2) \u8bb0\u4f4fsizeof(void *) != sizeof(int)\uff0c\u5982\u679c\u9700\u8981\u4e00\u4e2a\u6307\u9488\u5927\u5c0f\u7684\u6574\u6570\u8981\u4f7f\u7528intptr_t\u3002<\/p>\n<p>3) \u9700\u8981\u5bf9\u7ed3\u6784\u5bf9\u9f50\u52a0\u4ee5\u7559\u5fc3\uff0c\u5c24\u5176\u662f\u5bf9\u4e8e\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\u7684\u7ed3\u6784\u4f53\u3002\u572864\u4f4d\u7cfb\u7edf\u4e2d\uff0c\u4efb\u4f55\u62e5\u6709int64_t\/uint64_t\u6210\u5458\u7684\u7c7b\/\u7ed3\u6784\u4f53\u5c06\u9ed8\u8ba4\u88ab\u5904\u7406\u4e3a8\u5b57\u8282\u5bf9\u9f50\u3002\u5982\u679c32\u4f4d\u548c64\u4f4d\u4ee3\u7801\u5171\u7528\u78c1\u76d8\u4e0a\u7684\u7ed3\u6784\u4f53\uff0c\u9700\u8981\u786e\u4fdd\u4e24\u79cd\u4f53\u7cfb\u7ed3\u6784\u4e0b\u7684\u7ed3\u6784\u4f53\u7684\u5bf9\u9f50\u4e00\u81f4\u3002\u5927\u591a\u6570\u7f16\u8bd1\u5668\u63d0\u4f9b\u4e86\u8c03\u6574\u7ed3\u6784\u4f53\u5bf9\u9f50\u7684\u65b9\u6848\u3002gcc\u4e2d\u53ef\u4f7f\u7528__attribute__((packed))\uff0cMSVC\u63d0\u4f9b\u4e86#pragma pack()\u548c__declspec(align())\uff08\u8bd1\u8005\u6ce8\uff0c\u89e3\u51b3\u65b9\u6848\u7684\u9879\u76ee\u5c5e\u6027\u91cc\u4e5f\u53ef\u4ee5\u76f4\u63a5\u8bbe\u7f6e\uff09\u3002<\/p>\n<p>4) \u521b\u5efa64\u4f4d\u5e38\u91cf\u65f6\u4f7f\u7528LL\u6216ULL\u4f5c\u4e3a\u540e\u7f00\uff0c\u5982\uff1a<\/p>\n<p>int64_t my_value = 0x123456789LL;<\/p>\n<p>uint64_t my_mask = 3ULL &lt;&lt; 48;<\/p>\n<p>5) \u5982\u679c\u4f60\u786e\u5b9e\u9700\u898132\u4f4d\u548c64\u4f4d\u7cfb\u7edf\u5177\u6709\u4e0d\u540c\u4ee3\u7801\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u53d8\u91cf\u524d\u4f7f\u7528\u3002\uff08\u5c3d\u91cf\u4e0d\u8981\u8fd9\u4e48\u505a\uff0c\u4f7f\u7528\u65f6\u5c3d\u91cf\u4f7f\u4fee\u6539\u5c40\u90e8\u5316\uff09\u3002<\/p>\n<h2>14. \u9884\u5904\u7406\u5b8f\uff08Preprocessor Macros\uff09<\/h2>\n<p>\u4f7f\u7528\u5b8f\u65f6\u8981\u8c28\u614e\uff0c\u5c3d\u91cf\u4ee5\u5185\u8054\u51fd\u6570\u3001\u679a\u4e3e\u548c\u5e38\u91cf\u4ee3\u66ff\u4e4b\u3002<\/p>\n<p>\u5b8f\u610f\u5473\u7740\u4f60\u548c\u7f16\u8bd1\u5668\u770b\u5230\u7684\u4ee3\u7801\u662f\u4e0d\u540c\u7684\uff0c\u56e0\u6b64\u53ef\u80fd\u5bfc\u81f4\u5f02\u5e38\u884c\u4e3a\uff0c\u5c24\u5176\u662f\u5f53\u5b8f\u5b58\u5728\u4e8e\u5168\u5c40\u4f5c\u7528\u57df\u4e2d\u3002<\/p>\n<p>\u503c\u5f97\u5e86\u5e78\u7684\u662f\uff0cC++\u4e2d\uff0c\u5b8f\u4e0d\u50cfC\u4e2d\u90a3\u4e48\u5fc5\u8981\u3002\u5b8f\u5185\u8054\u6548\u7387\u5173\u952e\u4ee3\u7801\uff08performance-critical code\uff09\u53ef\u4ee5\u5185\u8054\u51fd\u6570\u66ff\u4ee3\uff1b\u5b8f\u5b58\u50a8\u5e38\u91cf\u53ef\u4ee5const\u53d8\u91cf\u66ff\u4ee3\uff1b\u5b8f\u201c\u7f29\u5199\u201d\u957f\u53d8\u91cf\u540d\u53ef\u4ee5\u5f15\u7528\u66ff\u4ee3\uff1b\u4f7f\u7528\u5b8f\u8fdb\u884c\u6761\u4ef6\u7f16\u8bd1\uff0c\u8fd9\u4e2a\u2026\u2026\uff0c\u6700\u597d\u4e0d\u8981\u8fd9\u4e48\u505a\uff0c\u4f1a\u4ee4\u6d4b\u8bd5\u66f4\u52a0\u75db\u82e6\uff08#define\u9632\u6b62\u5934\u6587\u4ef6\u91cd\u5305\u542b\u5f53\u7136\u662f\u4e2a\u4f8b\u5916\uff09\u3002<\/p>\n<p>\u5b8f\u53ef\u4ee5\u505a\u4e00\u4e9b\u5176\u4ed6\u6280\u672f\u65e0\u6cd5\u5b9e\u73b0\u7684\u4e8b\u60c5\uff0c\u5728\u4e00\u4e9b\u4ee3\u7801\u5e93\uff08\u5c24\u5176\u662f\u5e95\u5c42\u5e93\u4e2d\uff09\u53ef\u4ee5\u770b\u5230\u5b8f\u7684\u67d0\u4e9b\u7279\u6027\uff08\u5982\u5b57\u7b26\u4e32\u5316\uff08stringifying\uff0c\u8bd1\u8005\u6ce8\uff0c\u4f7f\u7528#\uff09\u3001\u8fde\u63a5\uff08concatenation\uff0c\u8bd1\u8005\u6ce8\uff0c\u4f7f\u7528##\uff09\u7b49\u7b49\uff09\u3002\u4f46\u5728\u4f7f\u7528\u524d\uff0c\u4ed4\u7ec6\u8003\u8651\u4e00\u4e0b\u80fd\u4e0d\u80fd\u4e0d\u4f7f\u7528\u5b8f\u5b9e\u73b0\u540c\u6837\u6548\u679c\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u5173\u4e8e\u5b8f\u7684\u9ad8\u7ea7\u5e94\u7528\uff0c\u53ef\u4ee5\u53c2\u8003<\/strong><a href=\"http:\/\/milton.bloghome.cn\/posts\/171674.html\"><strong>C<\/strong><strong>\u8bed\u8a00\u5b8f\u7684\u9ad8\u7ea7\u5e94\u7528<\/strong><\/a><strong>\u3002<\/strong><\/p>\n<p>\u4e0b\u9762\u7ed9\u51fa\u7684\u7528\u6cd5\u6a21\u5f0f\u53ef\u4ee5\u907f\u514d\u4e00\u4e9b\u4f7f\u7528\u5b8f\u7684\u95ee\u9898\uff0c\u4f9b\u4f7f\u7528\u5b8f\u65f6\u53c2\u8003\uff1a<\/p>\n<p>1) \u4e0d\u8981\u5728.h\u6587\u4ef6\u4e2d\u5b9a\u4e49\u5b8f\uff1b<\/p>\n<p>2) \u4f7f\u7528\u524d\u6b63\u786e#define\uff0c\u4f7f\u7528\u540e\u6b63\u786e#undef\uff1b<\/p>\n<p>3) \u4e0d\u8981\u53ea\u662f\u5bf9\u5df2\u7ecf\u5b58\u5728\u7684\u5b8f\u4f7f\u7528#undef\uff0c\u9009\u62e9\u4e00\u4e2a\u4e0d\u4f1a\u51b2\u7a81\u7684\u540d\u79f0\uff1b<\/p>\n<p>4) \u4e0d\u4f7f\u7528\u4f1a\u5bfc\u81f4\u4e0d\u7a33\u5b9a\u7684C++\u6784\u9020\uff08unbalanced C++ constructs\uff0c\u8bd1\u8005\u6ce8\uff09\u7684\u5b8f\uff0c\u81f3\u5c11\u6587\u6863\u8bf4\u660e\u5176\u884c\u4e3a\u3002<\/p>\n<h2>15. 0\u548cNULL\uff080 and NULL\uff09<\/h2>\n<p>\u6574\u6570\u75280\uff0c\u5b9e\u6570\u75280.0\uff0c\u6307\u9488\u7528NULL\uff0c\u5b57\u7b26\uff08\u4e32\uff09\u7528&#8217;\\0&#8217;\u3002<\/p>\n<p>\u6574\u6570\u75280\uff0c\u5b9e\u6570\u75280.0\uff0c\u8fd9\u4e00\u70b9\u662f\u6beb\u65e0\u4e89\u8bae\u7684\u3002<\/p>\n<p>\u5bf9\u4e8e\u6307\u9488\uff08\u5730\u5740\u503c\uff09\uff0c\u5230\u5e95\u662f\u75280\u8fd8\u662fNULL\uff0cBjarne Stroustrup\u5efa\u8bae\u4f7f\u7528\u6700\u539f\u59cb\u76840\uff0c\u6211\u4eec\u5efa\u8bae\u4f7f\u7528\u770b\u4e0a\u53bb\u50cf\u662f\u6307\u9488\u7684NULL\uff0c\u4e8b\u5b9e\u4e0a\u4e00\u4e9bC++\u7f16\u8bd1\u5668\uff08\u5982gcc 4.1.0\uff09\u4e13\u95e8\u63d0\u4f9b\u4e86NULL\u7684\u5b9a\u4e49\uff0c\u53ef\u4ee5\u7ed9\u51fa\u6709\u7528\u7684\u8b66\u544a\uff0c\u5c24\u5176\u662fsizeof(NULL)\u548csizeof(0)\u4e0d\u76f8\u7b49\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5b57\u7b26\uff08\u4e32\uff09\u7528&#8217;\\0&#8217;\uff0c\u4e0d\u4ec5\u7c7b\u578b\u6b63\u786e\u800c\u4e14\u53ef\u8bfb\u6027\u597d\u3002<\/p>\n<h2>16. sizeof\uff08sizeof\uff09<\/h2>\n<p>\u5c3d\u53ef\u80fd\u7528sizeof(<em>varname<\/em>)\u4ee3\u66ffsizeof(<em>type<\/em>)\u3002<\/p>\n<p>\u4f7f\u7528sizeof(<em>varname<\/em>)\u662f\u56e0\u4e3a\u5f53\u53d8\u91cf\u7c7b\u578b\u6539\u53d8\u65f6\u4ee3\u7801\u81ea\u52a8\u540c\u6b65\uff0c\u6709\u4e9b\u60c5\u51b5\u4e0bsizeof(<em>type<\/em>)\u6216\u8bb8\u6709\u610f\u4e49\uff0c\u8fd8\u662f\u8981\u5c3d\u91cf\u907f\u514d\uff0c\u5982\u679c\u53d8\u91cf\u7c7b\u578b\u6539\u53d8\u7684\u8bdd\u4e0d\u80fd\u540c\u6b65\u3002<\/p>\n<p>Struct data;<\/p>\n<p>memset(&amp;data, 0, sizeof(data));<\/p>\n<p>memset(&amp;data, 0, sizeof(Struct));<\/p>\n<h2>17. Boost\u5e93\uff08Boost\uff09<\/h2>\n<p>\u53ea\u4f7f\u7528Boost\u4e2d\u88ab\u8ba4\u53ef\u7684\u5e93\u3002<\/p>\n<p><strong>\u5b9a\u4e49<\/strong>\uff1aBoost\u5e93\u96c6\u662f\u4e00\u4e2a\u975e\u5e38\u53d7\u6b22\u8fce\u7684\u3001\u540c\u7ea7\u8bc4\u8bae\u7684\uff08peer-reviewed\uff09\u3001\u514d\u8d39\u7684\u3001\u5f00\u6e90\u7684C++\u5e93\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1aBoost\u4ee3\u7801\u8d28\u91cf\u666e\u904d\u8f83\u9ad8\u3001\u53ef\u79fb\u690d\u6027\u597d\uff0c\u586b\u8865\u4e86C++\u6807\u51c6\u5e93\u5f88\u591a\u7a7a\u767d\uff0c\u5982\u578b\u522b\u7279\u6027\uff08type traits\uff09\u3001\u66f4\u5b8c\u5584\u7684\u7ed1\u5b9a\uff08binders\uff09\u3001\u66f4\u597d\u7684\u667a\u80fd\u6307\u9488\uff0c\u540c\u65f6\u8fd8\u63d0\u4f9b\u4e86TR1\uff08\u6807\u51c6\u5e93\u7684\u6269\u5c55\uff09\u7684\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u67d0\u4e9bBoost\u5e93\u63d0\u5021\u7684\u7f16\u7a0b\u5b9e\u8df5\u53ef\u8bfb\u6027\u5dee\uff0c\u50cf\u5143\u7a0b\u5e8f\uff08metaprogramming\uff09\u548c\u5176\u4ed6\u9ad8\u7ea7\u6a21\u677f\u6280\u672f\uff0c\u4ee5\u53ca\u8fc7\u5ea6\u201c\u51fd\u6570\u5316\u201d\uff08&#8221;functional&#8221;\uff09\u7684\u7f16\u7a0b\u98ce\u683c\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a\u4e3a\u4e86\u5411\u9605\u8bfb\u548c\u7ef4\u62a4\u4ee3\u7801\u7684\u4eba\u5458\u63d0\u4f9b\u66f4\u597d\u7684\u53ef\u8bfb\u6027\uff0c\u6211\u4eec\u53ea\u5141\u8bb8\u4f7f\u7528Boost\u7279\u6027\u7684\u4e00\u4e2a\u6210\u719f\u5b50\u96c6\uff0c\u5f53\u524d\uff0c\u8fd9\u4e9b\u5e93\u5305\u62ec\uff1a<\/p>\n<p>1) <a href=\"http:\/\/www.boost.org\/libs\/utility\/compressed_pair.htm\">Compressed Pair<\/a>\uff1aboost\/compressed_pair.hpp\uff1b<\/p>\n<p>2) <a href=\"http:\/\/www.boost.org\/libs\/ptr_container\/\">Pointer Container<\/a>\uff1aboost\/ptr_container\u4e0d\u5305\u62ecptr_array.hpp\u548c\u5e8f\u5217\u5316\uff08serialization\uff09\u3002<\/p>\n<p>\u6211\u4eec\u4f1a\u79ef\u6781\u8003\u8651\u6dfb\u52a0\u53ef\u4ee5\u7684Boost\u7279\u6027\uff0c\u6240\u4ee5\u4e0d\u5fc5\u62d8\u6ce5\u4e8e\u8be5\u89c4\u5219\u3002<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u5173\u4e8eC++\u7279\u6027\u7684\u6ce8\u610f\u4e8b\u9879\uff0c\u603b\u7ed3\u4e00\u4e0b\uff1a<\/p>\n<ol>\n<li><strong>\u5bf9\u4e8e\u667a\u80fd\u6307\u9488\uff0c\u5b89\u5168\u7b2c\u4e00\u3001\u65b9\u4fbf\u7b2c\u4e8c\uff0c\u5c3d\u53ef\u80fd\u5c40\u90e8\u5316\uff08<\/strong><strong>scoped_ptr<\/strong><strong>\uff09\uff1b<\/strong><\/li>\n<li><strong>\u5f15\u7528\u5f62\u53c2\u52a0\u4e0a<\/strong><strong>const<\/strong><strong>\uff0c\u5426\u5219\u4f7f\u7528\u6307\u9488\u5f62\u53c2\uff1b<\/strong><\/li>\n<li><strong>\u51fd\u6570\u91cd\u8f7d\u7684\u4f7f\u7528\u8981\u6e05\u6670\u3001\u6613\u8bfb\uff1b<\/strong><\/li>\n<li><strong>\u9274\u4e8e\u5bb9\u6613\u8bef\u7528\uff0c\u7981\u6b62\u4f7f\u7528\u7f3a\u7701\u51fd\u6570\u53c2\u6570\uff08\u503c\u5f97\u5546\u69b7\uff09\uff1b<\/strong><\/li>\n<li><strong>\u7981\u6b62\u4f7f\u7528\u53d8\u957f\u6570\u7ec4\uff1b<\/strong><\/li>\n<li><strong>\u5408\u7406\u4f7f\u7528\u53cb\u5143\uff1b<\/strong><\/li>\n<li><strong>\u4e3a\u4e86\u65b9\u4fbf\u4ee3\u7801\u7ba1\u7406\uff0c\u7981\u6b62\u4f7f\u7528\u5f02\u5e38\uff08\u503c\u5f97\u5546\u69b7\uff09\uff1b<\/strong><\/li>\n<li><strong>\u7981\u6b62\u4f7f\u7528<\/strong><strong>RTTI<\/strong><strong>\uff0c\u5426\u5219\u91cd\u65b0\u8bbe\u8ba1\u4ee3\u7801\u5427\uff1b<\/strong><\/li>\n<li><strong>\u4f7f\u7528<\/strong><strong>C++<\/strong><strong>\u98ce\u683c\u7684\u7c7b\u578b\u8f6c\u6362\uff0c\u9664\u5355\u5143\u6d4b\u8bd5\u5916\u4e0d\u8981\u4f7f\u7528<\/strong><strong>dynamic_cast<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u4f7f\u7528\u6d41\u8fd8<\/strong><strong>printf + read\/write<\/strong><strong>\uff0c<\/strong><strong>it is a problem<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u80fd\u7528\u524d\u7f6e\u81ea\u589e<\/strong><strong>\/<\/strong><strong>\u51cf\u4e0d\u7528\u540e\u7f6e\u81ea\u589e<\/strong><strong>\/<\/strong><strong>\u51cf\uff1b<\/strong><\/li>\n<li><strong> const<\/strong><strong>\u80fd\u7528\u5219\u7528\uff0c\u63d0\u5021<\/strong><strong>const<\/strong><strong>\u5728\u524d\uff1b<\/strong><\/li>\n<li><strong>\u4f7f\u7528\u786e\u5b9a\u5927\u5c0f\u7684\u6574\u578b\uff0c\u9664\u4f4d\u7ec4\u5916\u4e0d\u8981\u4f7f\u7528\u65e0\u7b26\u53f7\u578b\uff1b<\/strong><\/li>\n<li><strong>\u683c\u5f0f\u5316\u8f93\u51fa\u53ca\u7ed3\u6784\u5bf9\u9f50\u65f6\uff0c\u6ce8\u610f<\/strong><strong>32<\/strong><strong>\u4f4d\u548c<\/strong><strong>64<\/strong><strong>\u4f4d\u7684\u7cfb\u7edf\u5dee\u5f02\uff1b<\/strong><\/li>\n<li><strong>\u9664\u5b57\u7b26\u4e32\u5316\u3001\u8fde\u63a5\u5916\u5c3d\u91cf\u907f\u514d\u4f7f\u7528\u5b8f\uff1b<\/strong><\/li>\n<li><strong>\u6574\u6570\u7528<\/strong><strong>0<\/strong><strong>\uff0c\u5b9e\u6570\u7528<\/strong><strong>0.0<\/strong><strong>\uff0c\u6307\u9488\u7528<\/strong><strong>NULL<\/strong><strong>\uff0c\u5b57\u7b26\uff08\u4e32\uff09\u7528<\/strong><strong>&#8216;\\0&#8217;<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u7528<\/strong><strong>sizeof(varname)<\/strong><strong>\u4ee3\u66ff<\/strong><strong>sizeof(type)<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u53ea\u4f7f\u7528<\/strong><strong>Boost<\/strong><strong>\u4e2d\u88ab\u8ba4\u53ef\u7684\u5e93\u3002<\/strong><\/li>\n<\/ol>\n<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/22\/56845.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e94\uff09<\/a><\/h1>\n<h2>\u547d\u540d\u7ea6\u5b9a<\/h2>\n<p>\u6700\u91cd\u8981\u7684\u4e00\u81f4\u6027\u89c4\u5219\u662f\u547d\u540d\u7ba1\u7406\uff0c\u547d\u540d\u98ce\u683c\u76f4\u63a5\u53ef\u4ee5\u76f4\u63a5\u786e\u5b9a\u547d\u540d\u5b9e\u4f53\u662f\uff1a\u7c7b\u578b\u3001\u53d8\u91cf\u3001\u51fd\u6570\u3001\u5e38\u91cf\u3001\u5b8f\u7b49\u7b49\uff0c\u65e0\u9700\u67e5\u627e\u5b9e\u4f53\u58f0\u660e\uff0c\u6211\u4eec\u5927\u8111\u4e2d\u7684\u6a21\u5f0f\u5339\u914d\u5f15\u64ce\u4f9d\u8d56\u4e8e\u8fd9\u4e9b\u547d\u540d\u89c4\u5219\u3002<\/p>\n<p>\u547d\u540d\u89c4\u5219\u5177\u6709\u4e00\u5b9a\u968f\u610f\u6027\uff0c\u4f46\u76f8\u6bd4\u6309\u4e2a\u4eba\u559c\u597d\u547d\u540d\uff0c\u4e00\u81f4\u6027\u66f4\u91cd\u8981\uff0c\u6240\u4ee5\u4e0d\u7ba1\u4f60\u600e\u4e48\u60f3\uff0c\u89c4\u5219\u603b\u5f52\u662f\u89c4\u5219\u3002<\/p>\n<h2>1. \u901a\u7528\u547d\u540d\u89c4\u5219\uff08General Naming Rules\uff09<\/h2>\n<p>\u51fd\u6570\u547d\u540d\u3001\u53d8\u91cf\u547d\u540d\u3001\u6587\u4ef6\u547d\u540d\u5e94\u5177\u6709\u63cf\u8ff0\u6027\uff0c\u4e0d\u8981\u8fc7\u5ea6\u7f29\u5199\uff0c\u7c7b\u578b\u548c\u53d8\u91cf\u5e94\u8be5\u662f\u540d\u8bcd\uff0c\u51fd\u6570\u540d\u53ef\u4ee5\u7528\u201c\u547d\u4ee4\u6027\u201d\u52a8\u8bcd\u3002<\/p>\n<p><strong>\u5982\u4f55\u547d\u540d<\/strong>\uff1a<\/p>\n<p>\u5c3d\u53ef\u80fd\u7ed9\u51fa\u63cf\u8ff0\u6027\u540d\u79f0\uff0c\u4e0d\u8981\u8282\u7ea6\u7a7a\u95f4\uff0c\u8ba9\u522b\u4eba\u5f88\u5feb\u7406\u89e3\u4f60\u7684\u4ee3\u7801\u66f4\u91cd\u8981\uff0c\u597d\u7684\u547d\u540d\u9009\u62e9\uff1a<\/p>\n<p>int num_errors;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Good.<\/p>\n<p>int num_completed_connections;\u00a0\u00a0 \/\/ Good.<\/p>\n<p>\u4e11\u964b\u7684\u547d\u540d\u4f7f\u7528\u6a21\u7cca\u7684\u7f29\u5199\u6216\u968f\u610f\u7684\u5b57\u7b26\uff1a<\/p>\n<p>int n;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Bad &#8211; meaningless.<\/p>\n<p>int nerr;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Bad &#8211; ambiguous abbreviation.<\/p>\n<p>int n_comp_conns;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Bad &#8211; ambiguous abbreviation.<\/p>\n<p>\u7c7b\u578b\u548c\u53d8\u91cf\u540d\u4e00\u822c\u4e3a\u540d\u8bcd\uff1a\u5982FileOpener\u3001num_errors\u3002<\/p>\n<p>\u51fd\u6570\u540d\u901a\u5e38\u662f\u6307\u4ee4\u6027\u7684\uff0c\u5982OpenFile()\u3001set_num_errors()\uff0c\u8bbf\u95ee\u51fd\u6570\u9700\u8981\u63cf\u8ff0\u7684\u66f4\u7ec6\u81f4\uff0c\u8981\u4e0e\u5176\u8bbf\u95ee\u7684\u53d8\u91cf\u76f8\u543b\u5408\u3002<\/p>\n<p><strong>\u7f29\u5199<\/strong>\uff1a<\/p>\n<p>\u9664\u975e\u653e\u5230\u9879\u76ee\u5916\u4e5f\u975e\u5e38\u660e\u4e86\uff0c\u5426\u5219\u4e0d\u8981\u4f7f\u7528\u7f29\u5199\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>\/\/ Good<\/p>\n<p>\/\/ These show proper names with no abbreviations.<\/p>\n<p>int num_dns_connections;\u00a0 \/\/ Most people know what &#8220;DNS&#8221; stands for.<\/p>\n<p>int price_count_reader;\u00a0\u00a0 \/\/ OK, price count. Makes sense.<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Bad!<\/p>\n<p>\/\/ Abbreviations can be confusing or ambiguous outside a small group.<\/p>\n<p>int wgc_connections;\u00a0 \/\/ Only your group knows what this stands for.<\/p>\n<p>int pc_reader;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Lots of things can be abbreviated &#8220;pc&#8221;.<\/p>\n<p>\u4e0d\u8981\u7528\u7701\u7565\u5b57\u6bcd\u7684\u7f29\u5199\uff1a<\/p>\n<p>int error_count;\u00a0 \/\/ Good.<\/p>\n<p>int error_cnt;\u00a0\u00a0\u00a0 \/\/ Bad.<\/p>\n<h2>2. \u6587\u4ef6\u547d\u540d\uff08File Names\uff09<\/h2>\n<p>\u6587\u4ef6\u540d\u8981\u5168\u90e8\u5c0f\u5199\uff0c\u53ef\u4ee5\u5305\u542b\u4e0b\u5212\u7ebf\uff08_\uff09\u6216\u77ed\u7ebf\uff08-\uff09\uff0c\u6309\u9879\u76ee\u7ea6\u5b9a\u6765\u3002<\/p>\n<p>\u53ef\u63a5\u53d7\u7684\u6587\u4ef6\u547d\u540d\uff1a<\/p>\n<p>my_useful_class.cc<\/p>\n<p>my-useful-class.cc<\/p>\n<p>myusefulclass.cc<\/p>\n<p>C++\u6587\u4ef6\u4ee5.cc\u7ed3\u5c3e\uff0c\u5934\u6587\u4ef6\u4ee5.h\u7ed3\u5c3e\u3002<\/p>\n<p>\u4e0d\u8981\u4f7f\u7528\u5df2\u7ecf\u5b58\u5728\u4e8e\/usr\/include\u4e0b\u7684\u6587\u4ef6\u540d\uff08\u8bd1\u8005\u6ce8\uff0c\u5bf9UNIX\u3001Linux\u7b49\u7cfb\u7edf\u800c\u8a00\uff09\uff0c\u5982db.h\u3002<\/p>\n<p>\u901a\u5e38\uff0c\u5c3d\u91cf\u8ba9\u6587\u4ef6\u540d\u66f4\u52a0\u660e\u786e\uff0chttp_server_logs.h\u5c31\u6bd4logs.h\u8981\u597d\uff0c\u5b9a\u4e49\u7c7b\u65f6\u6587\u4ef6\u540d\u4e00\u822c\u6210\u5bf9\u51fa\u73b0\uff0c\u5982foo_bar.h\u548cfoo_bar.cc\uff0c\u5bf9\u5e94\u7c7bFooBar\u3002<\/p>\n<p>\u5185\u8054\u51fd\u6570\u5fc5\u987b\u653e\u5728.h\u6587\u4ef6\u4e2d\uff0c\u5982\u679c\u5185\u8054\u51fd\u6570\u6bd4\u8f83\u77ed\uff0c\u5c31\u76f4\u63a5\u653e\u5728.h\u4e2d\u3002\u5982\u679c\u4ee3\u7801\u6bd4\u8f83\u957f\uff0c\u53ef\u4ee5\u653e\u5230\u4ee5-inl.h\u7ed3\u5c3e\u7684\u6587\u4ef6\u4e2d\u3002\u5bf9\u4e8e\u5305\u542b\u5927\u91cf\u5185\u8054\u4ee3\u7801\u7684\u7c7b\uff0c\u53ef\u4ee5\u6709\u4e09\u4e2a\u6587\u4ef6\uff1a<\/p>\n<p>url_table.h\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ The class declaration.<\/p>\n<p>url_table.cc\u00a0\u00a0\u00a0\u00a0 \/\/ The class definition.<\/p>\n<p>url_table-inl.h\u00a0 \/\/ Inline functions that include lots of code.<\/p>\n<p>\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/10\/55818.html\">\u7b2c\u4e00\u7bc7<\/a><strong>-inl.h<\/strong><strong>\u6587\u4ef6<\/strong>\u4e00\u8282\u3002<\/p>\n<h2>3. \u7c7b\u578b\u547d\u540d\uff08Type Names\uff09<\/h2>\n<p>\u7c7b\u578b\u547d\u540d\u6bcf\u4e2a\u5355\u8bcd\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u4e0d\u5305\u542b\u4e0b\u5212\u7ebf\uff1aMyExcitingClass\u3001MyExcitingEnum\u3002<\/p>\n<p>\u6240\u6709\u7c7b\u578b\u547d\u540d\u2014\u2014\u7c7b\u3001\u7ed3\u6784\u4f53\u3001\u7c7b\u578b\u5b9a\u4e49\uff08typedef\uff09\u3001\u679a\u4e3e\u2014\u2014\u4f7f\u7528\u76f8\u540c\u7ea6\u5b9a\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>\/\/ classes and structs<\/p>\n<p>class UrlTable { &#8230;<\/p>\n<p>class UrlTableTester { &#8230;<\/p>\n<p>struct UrlTableProperties { &#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ typedefs<\/p>\n<p>typedef hash_map&lt;UrlTableProperties *, string&gt; PropertiesMap;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ enums<\/p>\n<p>enum UrlTableErrors { &#8230;<\/p>\n<h2>4. \u53d8\u91cf\u547d\u540d\uff08Variable Names\uff09<\/h2>\n<p>\u53d8\u91cf\u540d\u4e00\u5f8b\u5c0f\u5199\uff0c\u5355\u8bcd\u95f4\u4ee5\u4e0b\u5212\u7ebf\u76f8\u8fde\uff0c\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u4ee5\u4e0b\u5212\u7ebf\u7ed3\u5c3e\uff0c\u5982my_exciting_local_variable\u3001my_exciting_member_variable_\u3002<\/p>\n<p><strong>\u666e\u901a\u53d8\u91cf\u547d\u540d<\/strong>\uff1a<\/p>\n<p>\u4e3e\u4f8b\uff1a<\/p>\n<p>string table_name;\u00a0 \/\/ OK &#8211; uses underscore.<\/p>\n<p>string tablename;\u00a0\u00a0 \/\/ OK &#8211; all lowercase.<\/p>\n<p>string tableName;\u00a0\u00a0 \/\/ Bad &#8211; mixed case.<\/p>\n<p><strong>\u7c7b\u6570\u636e\u6210\u5458<\/strong>\uff1a<\/p>\n<p>\u7ed3\u6784\u4f53\u7684\u6570\u636e\u6210\u5458\u53ef\u4ee5\u548c\u666e\u901a\u53d8\u91cf\u4e00\u6837\uff0c\u4e0d\u7528\u50cf\u7c7b\u90a3\u6837\u63a5\u4e0b\u5212\u7ebf\uff1a<\/p>\n<p>struct UrlTableProperties {<\/p>\n<p>string name;<\/p>\n<p>int num_entries;<\/p>\n<p>}<\/p>\n<p>\u7ed3\u6784\u4f53\u4e0e\u7c7b\u7684\u8ba8\u8bba\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/16\/56324.html\">\u7b2c\u4e09\u7bc7<\/a><strong>\u7ed3\u6784\u4f53<\/strong><strong>vs.<\/strong><strong>\u7c7b<\/strong>\u4e00\u8282\u3002<\/p>\n<p><strong>\u5168\u5c40\u53d8\u91cf<\/strong>\uff1a<\/p>\n<p>\u5bf9\u5168\u5c40\u53d8\u91cf\u6ca1\u6709\u7279\u522b\u8981\u6c42\uff0c\u5c11\u7528\u5c31\u597d\uff0c\u53ef\u4ee5\u4ee5g_\u6216\u5176\u4ed6\u6613\u4e0e\u5c40\u90e8\u53d8\u91cf\u533a\u5206\u7684\u6807\u5fd7\u4e3a\u524d\u7f00\u3002<\/p>\n<h2>5. \u5e38\u91cf\u547d\u540d\uff08Constant Names\uff09<\/h2>\n<p>\u5728\u540d\u79f0\u524d\u52a0k\uff1akDaysInAWeek\u3002<\/p>\n<p>\u6240\u6709\u7f16\u8bd1\u65f6\u5e38\u91cf\uff08\u65e0\u8bba\u662f\u5c40\u90e8\u7684\u3001\u5168\u5c40\u7684\u8fd8\u662f\u7c7b\u4e2d\u7684\uff09\u548c\u5176\u4ed6\u53d8\u91cf\u4fdd\u6301\u4e9b\u8bb8\u533a\u522b\uff0ck\u540e\u63a5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\u7684\u5355\u8bcd\uff1a<\/p>\n<p>const int kDaysInAWeek = 7;<\/p>\n<h2>6. \u51fd\u6570\u547d\u540d\uff08Function Names\uff09<\/h2>\n<p>\u666e\u901a\u51fd\u6570\uff08regular functions\uff0c\u8bd1\u8005\u6ce8\uff0c\u8fd9\u91cc\u4e0e\u8bbf\u95ee\u51fd\u6570\u7b49\u7279\u6b8a\u51fd\u6570\u76f8\u5bf9\uff09\u5927\u5c0f\u5199\u6df7\u5408\uff0c\u5b58\u53d6\u51fd\u6570\uff08accessors and mutators\uff09\u5219\u8981\u6c42\u4e0e\u53d8\u91cf\u540d\u5339\u914d\uff1aMyExcitingFunction()\u3001MyExcitingMethod()\u3001my_exciting_member_variable()\u3001set_my_exciting_member_variable()\u3002<\/p>\n<p><strong>\u666e\u901a\u51fd\u6570<\/strong>\uff1a<\/p>\n<p>\u51fd\u6570\u540d\u4ee5\u5927\u5199\u5b57\u6bcd\u5f00\u5934\uff0c\u6bcf\u4e2a\u5355\u8bcd\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u6ca1\u6709\u4e0b\u5212\u7ebf\uff1a<\/p>\n<p>AddTableEntry()<\/p>\n<p>DeleteUrl()<\/p>\n<p><strong>\u5b58\u53d6\u51fd\u6570<\/strong>\uff1a<\/p>\n<p>\u5b58\u53d6\u51fd\u6570\u8981\u4e0e\u5b58\u53d6\u7684\u53d8\u91cf\u540d\u5339\u914d\uff0c\u8fd9\u513f\u6458\u5f55\u4e00\u4e2a\u62e5\u6709\u5b9e\u4f8b\u53d8\u91cfnum_entries_\u7684\u7c7b\uff1a<\/p>\n<p>class MyClass {<\/p>\n<p>public:<\/p>\n<p>&#8230;<\/p>\n<p>int num_entries() const { return num_entries_; }<\/p>\n<p>void set_num_entries(int num_entries) { num_entries_ = num_entries; }<\/p>\n<p>&nbsp;<\/p>\n<p>private:<\/p>\n<p>int num_entries_;<\/p>\n<p>};<\/p>\n<p>\u5176\u4ed6\u77ed\u5c0f\u7684\u5185\u8054\u51fd\u6570\u540d\u4e5f\u53ef\u4ee5\u4f7f\u7528\u5c0f\u5199\u5b57\u6bcd\uff0c\u4f8b\u5982\uff0c\u5728\u5faa\u73af\u4e2d\u8c03\u7528\u8fd9\u6837\u7684\u51fd\u6570\u751a\u81f3\u90fd\u4e0d\u9700\u8981\u7f13\u5b58\u5176\u503c\uff0c\u5c0f\u5199\u547d\u540d\u5c31\u53ef\u4ee5\u63a5\u53d7\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u4ece\u8fd9\u4e00\u70b9\u4e0a\u53ef\u4ee5\u770b\u51fa\uff0c\u5c0f\u5199\u7684\u51fd\u6570\u540d\u610f\u5473\u7740\u53ef\u4ee5\u76f4\u63a5\u5185\u8054\u4f7f\u7528\u3002<\/strong><\/p>\n<h2>7. \u547d\u540d\u7a7a\u95f4\uff08Namespace Names\uff09<\/h2>\n<p>\u547d\u540d\u7a7a\u95f4\u7684\u540d\u79f0\u662f\u5168\u5c0f\u5199\u7684\uff0c\u5176\u547d\u540d\u57fa\u4e8e\u9879\u76ee\u540d\u79f0\u548c\u76ee\u5f55\u7ed3\u6784\uff1agoogle_awesome_project\u3002<\/p>\n<p>\u5173\u4e8e\u547d\u540d\u7a7a\u95f4\u7684\u8ba8\u8bba\u548c\u5982\u4f55\u547d\u540d\uff0c\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/14\/56109.html\">\u7b2c\u4e8c\u7bc7<\/a><strong>\u547d\u540d\u7a7a\u95f4<\/strong>\u3002<\/p>\n<h2>8. \u679a\u4e3e\u547d\u540d\uff08Enumerator Names\uff09<\/h2>\n<p>\u679a\u4e3e\u503c\u5e94\u5168\u90e8\u5927\u5199\uff0c\u5355\u8bcd\u95f4\u4ee5\u4e0b\u5212\u7ebf\u76f8\u8fde\uff1aMY_EXCITING_ENUM_VALUE\u3002<\/p>\n<p>\u679a\u4e3e\u540d\u79f0\u5c5e\u4e8e\u7c7b\u578b\uff0c\u56e0\u6b64\u5927\u5c0f\u5199\u6df7\u5408\uff1aUrlTableErrors\u3002<\/p>\n<p>enum UrlTableErrors {<\/p>\n<p>OK = 0,<\/p>\n<p>ERROR_OUT_OF_MEMORY,<\/p>\n<p>ERROR_MALFORMED_INPUT,<\/p>\n<p>};<\/p>\n<h2>9. \u5b8f\u547d\u540d\uff08Macro Names\uff09<\/h2>\n<p>\u4f60\u5e76\u4e0d\u6253\u7b97\u4f7f\u7528\u5b8f\uff0c\u5bf9\u5427\uff1f\u5982\u679c\u4f7f\u7528\uff0c\u50cf\u8fd9\u6837\uff1aMY_MACRO_THAT_SCARES_SMALL_CHILDREN\u3002<\/p>\n<p>\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/21\/56760.html\">\u7b2c\u56db\u7bc7<\/a><strong>\u9884\u5904\u7406\u5b8f<\/strong>\uff0c\u901a\u5e38\u662f\u4e0d\u4f7f\u7528\u5b8f\u7684\uff0c\u5982\u679c\u7edd\u5bf9\u8981\u7528\uff0c\u5176\u547d\u540d\u50cf\u679a\u4e3e\u547d\u540d\u4e00\u6837\u5168\u90e8\u5927\u5199\u3001\u4f7f\u7528\u4e0b\u5212\u7ebf\uff1a<\/p>\n<p>#define ROUND(x) &#8230;<\/p>\n<p>#define PI_ROUNDED 3.0<\/p>\n<p>MY_EXCITING_ENUM_VALUE<\/p>\n<h2>10. \u547d\u540d\u89c4\u5219\u4f8b\u5916\uff08Exceptions to Naming Rules\uff09<\/h2>\n<p>\u5f53\u547d\u540d\u4e0e\u73b0\u6709C\/C++\u5b9e\u4f53\u76f8\u4f3c\u7684\u5bf9\u8c61\u65f6\uff0c\u53ef\u53c2\u8003\u73b0\u6709\u547d\u540d\u7ea6\u5b9a\uff1a<\/p>\n<p>bigopen()<\/p>\n<p>\u51fd\u6570\u540d\uff0c\u53c2\u8003open()<\/p>\n<p>uint<\/p>\n<p>typedef\u7c7b\u578b\u5b9a\u4e49<\/p>\n<p>bigpos<\/p>\n<p>struct\u6216class\uff0c\u53c2\u8003pos<\/p>\n<p>sparse_hash_map<\/p>\n<p>STL\u76f8\u4f3c\u5b9e\u4f53\uff1b\u53c2\u8003STL\u547d\u540d\u7ea6\u5b9a<\/p>\n<p>LONGLONG_MAX<\/p>\n<p>\u5e38\u91cf\uff0c\u7c7b\u4f3cINT_MAX<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u547d\u540d\u7ea6\u5b9a\u5c31\u76f8\u5bf9\u8f7b\u677e\u8bb8\u591a\uff0c\u5728\u9075\u4ece\u4ee3\u7801\u4e00\u81f4\u6027\u3001\u53ef\u8bfb\u6027\u7684\u524d\u63d0\u4e0b\uff0c\u7565\u663e\u968f\u610f\uff1a<\/p>\n<ol>\n<li><strong>\u603b\u4f53\u89c4\u5219\uff1a\u4e0d\u8981\u968f\u610f\u7f29\u5199\uff0c\u5982\u679c\u8bf4<\/strong><strong>ChangeLocalValue<\/strong><strong>\u5199\u4f5c<\/strong><strong>ChgLocVal<\/strong><strong>\u8fd8\u6709\u60c5\u53ef\u539f\u7684\u8bdd\uff0c\u628a<\/strong><strong>ModifyPlayerName<\/strong><strong>\u5199\u4f5c<\/strong><strong>MdfPlyNm<\/strong><strong>\u5c31\u592a\u8fc7\u5206\u4e86\uff0c\u9664\u51fd\u6570\u540d\u53ef\u9002\u5f53\u4e3a\u52a8\u8bcd\u5916\uff0c\u5176\u4ed6\u547d\u540d\u5c3d\u91cf\u4f7f\u7528\u6e05\u6670\u6613\u61c2\u7684\u540d\u8bcd\uff1b<\/strong><\/li>\n<li><strong>\u5b8f\u3001\u679a\u4e3e\u7b49\u4f7f\u7528\u5168\u90e8\u5927\u5199<\/strong><strong>+<\/strong><strong>\u4e0b\u5212\u7ebf\uff1b<\/strong><\/li>\n<li><strong>\u53d8\u91cf\uff08\u542b\u7c7b\u3001\u7ed3\u6784\u4f53\u6210\u5458\u53d8\u91cf\uff09\u3001\u6587\u4ef6\u3001\u547d\u540d\u7a7a\u95f4\u3001\u5b58\u53d6\u51fd\u6570\u7b49\u4f7f\u7528\u5168\u90e8\u5c0f\u5199<\/strong><strong>+<\/strong><strong>\u4e0b\u5212\u7ebf\uff0c\u7c7b\u6210\u5458\u53d8\u91cf\u4ee5\u4e0b\u5212\u7ebf\u7ed3\u5c3e\uff0c\u5168\u5c40\u53d8\u91cf\u4ee5<\/strong><strong>g_<\/strong><strong>\u5f00\u5934\uff1b<\/strong><\/li>\n<li><strong>\u666e\u901a\u51fd\u6570\u3001\u7c7b\u578b\uff08\u542b\u7c7b\u4e0e\u7ed3\u6784\u4f53\u3001\u679a\u4e3e\u7c7b\u578b\uff09\u3001\u5e38\u91cf\u7b49\u4f7f\u7528\u5927\u5c0f\u5199\u6df7\u5408\uff0c\u4e0d\u542b\u4e0b\u5212\u7ebf\uff1b<\/strong><\/li>\n<li><strong>\u53c2\u8003\u73b0\u6709\u6216\u76f8\u8fd1\u547d\u540d\u7ea6\u5b9a\u3002<\/strong><\/li>\n<\/ol>\n<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/22\/56873.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u516d\uff09<\/a><\/h1>\n<h2>\u6ce8\u91ca<\/h2>\n<p>\u6ce8\u91ca\u867d\u7136\u5199\u8d77\u6765\u5f88\u75db\u82e6\uff0c\u4f46\u5bf9\u4fdd\u8bc1\u4ee3\u7801\u53ef\u8bfb\u6027\u81f3\u4e3a\u91cd\u8981\uff0c\u4e0b\u9762\u7684\u89c4\u5219\u63cf\u8ff0\u4e86\u5e94\u8be5\u6ce8\u91ca\u4ec0\u4e48\u3001\u6ce8\u91ca\u5728\u54ea\u513f\u3002\u5f53\u7136\u4e5f\u8981\u8bb0\u4f4f\uff0c\u6ce8\u91ca\u7684\u786e\u5f88\u91cd\u8981\uff0c\u4f46\u6700\u597d\u7684\u4ee3\u7801\u672c\u8eab\u5c31\u662f\u6587\u6863\uff08self-documenting\uff09\uff0c\u7c7b\u578b\u548c\u53d8\u91cf\u547d\u540d\u610f\u4e49\u660e\u786e\u8981\u6bd4\u901a\u8fc7\u6ce8\u91ca\u89e3\u91ca\u6a21\u7cca\u7684\u547d\u540d\u597d\u5f97\u591a\u3002<\/p>\n<p>\u6ce8\u91ca\u662f\u4e3a\u522b\u4eba\uff08\u4e0b\u4e00\u4e2a\u9700\u8981\u7406\u89e3\u4f60\u7684\u4ee3\u7801\u7684\u4eba\uff09\u800c\u5199\u7684\uff0c\u8ba4\u771f\u70b9\u5427\uff0c\u90a3\u4e0b\u4e00\u4e2a\u4eba\u53ef\u80fd\u5c31\u662f\u4f60\uff01<\/p>\n<h2>1. \u6ce8\u91ca\u98ce\u683c\uff08Comment Style\uff09<\/h2>\n<p>\u4f7f\u7528\/\/\u6216\/* *\/\uff0c\u7edf\u4e00\u5c31\u597d\u3002<\/p>\n<p>\/\/\u6216\/* *\/\u90fd\u53ef\u4ee5\uff0c\/\/\u53ea\u662f\u7528\u7684\u66f4\u52a0\u5e7f\u6cdb\uff0c\u5728\u5982\u4f55\u6ce8\u91ca\u548c\u6ce8\u91ca\u98ce\u683c\u4e0a\u786e\u4fdd\u7edf\u4e00\u3002<\/p>\n<h2>2. \u6587\u4ef6\u6ce8\u91ca\uff08File Comments\uff09<\/h2>\n<p>\u5728\u6bcf\u4e00\u4e2a\u6587\u4ef6\u5f00\u5934\u52a0\u5165\u7248\u6743\u516c\u544a\uff0c\u7136\u540e\u662f\u6587\u4ef6\u5185\u5bb9\u63cf\u8ff0\u3002<\/p>\n<p><strong>\u6cd5\u5f8b\u516c\u544a\u548c\u4f5c\u8005\u4fe1\u606f<\/strong>\uff1a<\/p>\n<p>\u6bcf\u4e00\u6587\u4ef6\u5305\u542b\u4ee5\u4e0b\u9879\uff0c\u4f9d\u6b21\u662f\uff1a<\/p>\n<p>1) \u7248\u6743\uff08copyright statement\uff09\uff1a\u5982Copyright 2008 Google Inc.\uff1b<\/p>\n<p>2) \u8bb8\u53ef\u7248\u672c\uff08license boilerplate\uff09\uff1a\u4e3a\u9879\u76ee\u9009\u62e9\u5408\u9002\u7684\u8bb8\u53ef\u8bc1\u7248\u672c\uff0c\u5982<a href=\"http:\/\/en.wikipedia.org\/wiki\/Apache_2.0_licensing\">Apache 2.0<\/a>\u3001<a href=\"http:\/\/en.wikipedia.org\/wiki\/BSD_license\">BSD<\/a>\u3001<a href=\"http:\/\/en.wikipedia.org\/wiki\/GNU_Lesser_General_Public_License\">LGPL<\/a>\u3001<a href=\"http:\/\/en.wikipedia.org\/wiki\/GNU_General_Public_License\">GPL<\/a>\uff1b<\/p>\n<p>3) \u4f5c\u8005\uff08author line\uff09\uff1a\u6807\u8bc6\u6587\u4ef6\u7684\u539f\u59cb\u4f5c\u8005\u3002<\/p>\n<p>\u5982\u679c\u4f60\u5bf9\u5176\u4ed6\u4eba\u521b\u5efa\u7684\u6587\u4ef6\u505a\u4e86\u91cd\u5927\u4fee\u6539\uff0c\u5c06\u4f60\u7684\u4fe1\u606f\u6dfb\u52a0\u5230\u4f5c\u8005\u4fe1\u606f\u91cc\uff0c\u8fd9\u6837\u5f53\u5176\u4ed6\u4eba\u5bf9\u8be5\u6587\u4ef6\u6709\u7591\u95ee\u65f6\u53ef\u4ee5\u77e5\u9053\u8be5\u8054\u7cfb\u8c01\u3002<\/p>\n<p><strong>\u6587\u4ef6\u5185\u5bb9<\/strong>\uff1a<\/p>\n<p>\u6bcf\u4e00\u4e2a\u6587\u4ef6\u7248\u6743\u8bb8\u53ef\u53ca\u4f5c\u8005\u4fe1\u606f\u540e\uff0c\u90fd\u8981\u5bf9\u6587\u4ef6\u5185\u5bb9\u8fdb\u884c\u6ce8\u91ca\u8bf4\u660e\u3002<\/p>\n<p>\u901a\u5e38\uff0c.h\u6587\u4ef6\u8981\u5bf9\u6240\u58f0\u660e\u7684\u7c7b\u7684\u529f\u80fd\u548c\u7528\u6cd5\u4f5c\u7b80\u5355\u8bf4\u660e\uff0c.cc\u6587\u4ef6\u5305\u542b\u4e86\u66f4\u591a\u7684\u5b9e\u73b0\u7ec6\u8282\u6216\u7b97\u6cd5\u8ba8\u8bba\uff0c\u5982\u679c\u4f60\u611f\u89c9\u8fd9\u4e9b\u5b9e\u73b0\u7ec6\u8282\u6216\u7b97\u6cd5\u8ba8\u8bba\u5bf9\u4e8e\u9605\u8bfb\u6709\u5e2e\u52a9\uff0c\u53ef\u4ee5\u628a.cc\u4e2d\u7684\u6ce8\u91ca\u653e\u5230.h\u4e2d\uff0c\u5e76\u5728.cc\u4e2d\u6307\u51fa\u6587\u6863\u5728.h\u4e2d\u3002<\/p>\n<p>\u4e0d\u8981\u5355\u7eaf\u5728.h\u548c.cc\u95f4\u590d\u5236\u6ce8\u91ca\uff0c\u590d\u5236\u7684\u6ce8\u91ca\u504f\u79bb\u4e86\u5b9e\u9645\u610f\u4e49\u3002<\/p>\n<h2>3. \u7c7b\u6ce8\u91ca\uff08Class Comments\uff09<\/h2>\n<p>\u6bcf\u4e2a\u7c7b\u7684\u5b9a\u4e49\u8981\u9644\u7740\u63cf\u8ff0\u7c7b\u7684\u529f\u80fd\u548c\u7528\u6cd5\u7684\u6ce8\u91ca\u3002<\/p>\n<p>\/\/ Iterates over the contents of a GargantuanTable.\u00a0 Sample usage:<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 GargantuanTable_Iterator* iter = table-&gt;NewIterator();<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 for (iter-&gt;Seek(&#8220;foo&#8221;); !iter-&gt;done(); iter-&gt;Next()) {<\/p>\n<p>\/\/\u00a0\u00a0\u00a0\u00a0\u00a0 process(iter-&gt;key(), iter-&gt;value());<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 }<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 delete iter;<\/p>\n<p>class GargantuanTable_Iterator {<\/p>\n<p>&#8230;<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u4f60\u89c9\u5f97\u5df2\u7ecf\u5728\u6587\u4ef6\u9876\u90e8\u8be6\u7ec6\u63cf\u8ff0\u4e86\u8be5\u7c7b\uff0c\u60f3\u76f4\u63a5\u7b80\u5355\u7684\u6765\u4e0a\u4e00\u53e5\u201c\u5b8c\u6574\u63cf\u8ff0\u89c1\u6587\u4ef6\u9876\u90e8\u201d\u7684\u8bdd\uff0c\u8fd8\u662f\u591a\u5c11\u5728\u7c7b\u4e2d\u52a0\u70b9\u6ce8\u91ca\u5427\u3002<\/p>\n<p>\u5982\u679c\u7c7b\u6709\u4efb\u4f55\u540c\u6b65\u524d\u63d0\uff08synchronization assumptions\uff09\uff0c\u6587\u6863\u8bf4\u660e\u4e4b\u3002\u5982\u679c\u8be5\u7c7b\u7684\u5b9e\u4f8b\u53ef\u88ab\u591a\u7ebf\u7a0b\u8bbf\u95ee\uff0c\u4f7f\u7528\u65f6\u52a1\u5fc5\u6ce8\u610f\u6587\u6863\u8bf4\u660e\u3002<\/p>\n<h2>4. \u51fd\u6570\u6ce8\u91ca\uff08Function Comments\uff09<\/h2>\n<p>\u51fd\u6570\u58f0\u660e\u5904\u6ce8\u91ca\u63cf\u8ff0\u51fd\u6570\u529f\u80fd\uff0c\u5b9a\u4e49\u5904\u63cf\u8ff0\u51fd\u6570\u5b9e\u73b0\u3002<\/p>\n<p><strong>\u51fd\u6570\u58f0\u660e<\/strong>\uff1a<\/p>\n<p>\u6ce8\u91ca\u4e8e\u58f0\u660e\u4e4b\u524d\uff0c\u63cf\u8ff0\u51fd\u6570\u529f\u80fd\u53ca\u7528\u6cd5\uff0c\u6ce8\u91ca\u4f7f\u7528\u63cf\u8ff0\u5f0f\uff08&#8221;Opens the file&#8221;\uff09\u800c\u975e\u6307\u4ee4\u5f0f\uff08&#8221;Open the file&#8221;\uff09\uff1b\u6ce8\u91ca\u53ea\u662f\u4e3a\u4e86\u63cf\u8ff0\u51fd\u6570\u800c\u4e0d\u662f\u544a\u8bc9\u51fd\u6570\u505a\u4ec0\u4e48\u3002\u901a\u5e38\uff0c\u6ce8\u91ca\u4e0d\u4f1a\u63cf\u8ff0\u51fd\u6570\u5982\u4f55\u5b9e\u73b0\uff0c\u90a3\u662f\u5b9a\u4e49\u90e8\u5206\u7684\u4e8b\u60c5\u3002<\/p>\n<p>\u51fd\u6570\u58f0\u660e\u5904\u6ce8\u91ca\u7684\u5185\u5bb9\uff1a<\/p>\n<p>1) inputs\uff08\u8f93\u5165\uff09\u53caoutputs\uff08\u8f93\u51fa\uff09\uff1b<\/p>\n<p>2) \u5bf9\u7c7b\u6210\u5458\u51fd\u6570\u800c\u8a00\uff1a\u51fd\u6570\u8c03\u7528\u671f\u95f4\u5bf9\u8c61\u662f\u5426\u9700\u8981\u4fdd\u6301\u5f15\u7528\u53c2\u6570\uff0c\u662f\u5426\u4f1a\u91ca\u653e\u8fd9\u4e9b\u53c2\u6570\uff1b<\/p>\n<p>3) \u5982\u679c\u51fd\u6570\u5206\u914d\u4e86\u7a7a\u95f4\uff0c\u9700\u8981\u7531\u8c03\u7528\u8005\u91ca\u653e\uff1b<\/p>\n<p>4) \u53c2\u6570\u662f\u5426\u53ef\u4ee5\u4e3aNULL\uff1b<\/p>\n<p>5) \u662f\u5426\u5b58\u5728\u51fd\u6570\u4f7f\u7528\u7684\u6027\u80fd\u9690\u5fe7\uff08performance implications\uff09\uff1b<\/p>\n<p>6) \u5982\u679c\u51fd\u6570\u662f\u53ef\u91cd\u5165\u7684\uff08re-entrant\uff09\uff0c\u5176\u540c\u6b65\u524d\u63d0\uff08synchronization assumptions\uff09\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>\u4e3e\u4f8b\u5982\u4e0b\uff1a<\/p>\n<p>\/\/ Returns an iterator for this table.\u00a0 It is the client&#8217;s<\/p>\n<p>\/\/ responsibility to delete the iterator when it is done with it,<\/p>\n<p>\/\/ and it must not use the iterator once the GargantuanTable object<\/p>\n<p>\/\/ on which the iterator was created has been deleted.<\/p>\n<p>\/\/<\/p>\n<p>\/\/ The iterator is initially positioned at the beginning of the table.<\/p>\n<p>\/\/<\/p>\n<p>\/\/ This method is equivalent to:<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 Iterator* iter = table-&gt;NewIterator();<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 iter-&gt;Seek(&#8220;&#8221;);<\/p>\n<p>\/\/\u00a0\u00a0\u00a0 return iter;<\/p>\n<p>\/\/ If you are going to immediately seek to another place in the<\/p>\n<p>\/\/ returned iterator, it will be faster to use NewIterator()<\/p>\n<p>\/\/ and avoid the extra seek.<\/p>\n<p>Iterator* GetIterator() const;<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f46\u4e0d\u8981\u6709\u65e0\u8c13\u5197\u4f59\u6216\u663e\u800c\u6613\u89c1\u7684\u6ce8\u91ca\uff0c\u4e0b\u9762\u7684\u6ce8\u91ca\u5c31\u6ca1\u6709\u5fc5\u8981\u52a0\u4e0a\u201creturns false otherwise\u201d\uff0c\u56e0\u4e3a\u5df2\u7ecf\u6697\u542b\u5176\u4e2d\u4e86\uff1a<\/p>\n<p>\/\/ Returns true if the table cannot hold any more entries.<\/p>\n<p>bool IsTableFull();<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u91ca\u6784\u9020\/\u6790\u6784\u51fd\u6570\u65f6\uff0c\u8bb0\u4f4f\uff0c\u8bfb\u4ee3\u7801\u7684\u4eba\u77e5\u9053\u6784\u9020\/\u6790\u6784\u51fd\u6570\u662f\u4ec0\u4e48\uff0c\u6240\u4ee5\u201cdestroys this object\u201d\u8fd9\u6837\u7684\u6ce8\u91ca\u662f\u6ca1\u6709\u610f\u4e49\u7684\u3002\u8bf4\u660e\u6784\u9020\u51fd\u6570\u5bf9\u53c2\u6570\u505a\u4e86\u4ec0\u4e48\uff08\u4f8b\u5982\uff0c\u662f\u5426\u662f\u6307\u9488\u7684\u6240\u6709\u8005\uff09\u4ee5\u53ca\u6790\u6784\u51fd\u6570\u6e05\u7406\u4e86\u4ec0\u4e48\uff0c\u5982\u679c\u90fd\u662f\u65e0\u5173\u7d27\u8981\u7684\u5185\u5bb9\uff0c\u76f4\u63a5\u7701\u6389\u6ce8\u91ca\uff0c\u6790\u6784\u51fd\u6570\u524d\u6ca1\u6709\u6ce8\u91ca\u662f\u5f88\u6b63\u5e38\u7684\u3002<\/p>\n<p><strong>\u51fd\u6570\u5b9a\u4e49<\/strong>\uff1a<\/p>\n<p>\u6bcf\u4e2a\u51fd\u6570\u5b9a\u4e49\u65f6\u8981\u4ee5\u6ce8\u91ca\u8bf4\u660e\u51fd\u6570\u529f\u80fd\u548c\u5b9e\u73b0\u8981\u70b9\uff0c\u5982\u4f7f\u7528\u7684\u6f02\u4eae\u4ee3\u7801\u3001\u5b9e\u73b0\u7684\u7b80\u8981\u6b65\u9aa4\u3001\u5982\u6b64\u5b9e\u73b0\u7684\u7406\u7531\u3001\u4e3a\u4ec0\u4e48\u524d\u534a\u90e8\u5206\u8981\u52a0\u9501\u800c\u540e\u534a\u90e8\u5206\u4e0d\u9700\u8981\u3002<\/p>\n<p>\u4e0d\u8981\u4ece.h\u6587\u4ef6\u6216\u5176\u4ed6\u5730\u65b9\u7684\u51fd\u6570\u58f0\u660e\u5904\u76f4\u63a5\u590d\u5236\u6ce8\u91ca\uff0c\u7b80\u8981\u8bf4\u660e\u51fd\u6570\u529f\u80fd\u662f\u53ef\u4ee5\u7684\uff0c\u4f46\u91cd\u70b9\u8981\u653e\u5728\u5982\u4f55\u5b9e\u73b0\u4e0a\u3002<\/p>\n<h2>5. \u53d8\u91cf\u6ce8\u91ca\uff08Variable Comments\uff09<\/h2>\n<p>\u901a\u5e38\u53d8\u91cf\u540d\u672c\u8eab\u8db3\u4ee5\u5f88\u597d\u8bf4\u660e\u53d8\u91cf\u7528\u9014\uff0c\u7279\u5b9a\u60c5\u51b5\u4e0b\uff0c\u9700\u8981\u989d\u5916\u6ce8\u91ca\u8bf4\u660e\u3002<\/p>\n<p><strong>\u7c7b\u6570\u636e\u6210\u5458<\/strong>\uff1a<\/p>\n<p>\u6bcf\u4e2a\u7c7b\u6570\u636e\u6210\u5458\uff08\u4e5f\u53eb\u5b9e\u4f8b\u53d8\u91cf\u6216\u6210\u5458\u53d8\u91cf\uff09\u5e94\u6ce8\u91ca\u8bf4\u660e\u7528\u9014\uff0c\u5982\u679c\u53d8\u91cf\u53ef\u4ee5\u63a5\u53d7NULL\u6216-1\u7b49\u8b66\u6212\u503c\uff08sentinel values\uff09\uff0c\u987b\u8bf4\u660e\u4e4b\uff0c\u5982\uff1a<\/p>\n<p>private:<\/p>\n<p>\/\/ Keeps track of the total number of entries in the table.<\/p>\n<p>\/\/ Used to ensure we do not go over the limit. -1 means<\/p>\n<p>\/\/ that we don&#8217;t yet know how many entries the table has.<\/p>\n<p>int num_total_entries_;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u5168\u5c40\u53d8\u91cf\uff08\u5e38\u91cf\uff09<\/strong>\uff1a<\/p>\n<p>\u548c\u6570\u636e\u6210\u5458\u76f8\u4f3c\uff0c\u6240\u6709\u5168\u5c40\u53d8\u91cf\uff08\u5e38\u91cf\uff09\u4e5f\u5e94\u6ce8\u91ca\u8bf4\u660e\u542b\u4e49\u53ca\u7528\u9014\uff0c\u5982\uff1a<\/p>\n<p>\/\/ The total number of tests cases that we run through in this regression test.<\/p>\n<p>const int kNumTestCases = 6;<\/p>\n<p>&nbsp;<\/p>\n<h2>6. \u5b9e\u73b0\u6ce8\u91ca\uff08Implementation Comments\uff09<\/h2>\n<p>\u5bf9\u4e8e\u5b9e\u73b0\u4ee3\u7801\u4e2d\u5de7\u5999\u7684\u3001\u6666\u6da9\u7684\u3001\u6709\u8da3\u7684\u3001\u91cd\u8981\u7684\u5730\u65b9\u52a0\u4ee5\u6ce8\u91ca\u3002<\/p>\n<p><strong>\u4ee3\u7801\u524d\u6ce8\u91ca<\/strong>\uff1a<\/p>\n<p>\u51fa\u5f69\u7684\u6216\u590d\u6742\u7684\u4ee3\u7801\u5757\u524d\u8981\u52a0\u6ce8\u91ca\uff0c\u5982\uff1a<\/p>\n<p>\/\/ Divide result by two, taking into account that x<\/p>\n<p>\/\/ contains the carry from the add.<\/p>\n<p>for (int i = 0; i &lt; result-&gt;size(); i++) {<\/p>\n<p>x = (x &lt;&lt; 8) + (*result)[i];<\/p>\n<p>(*result)[i] = x &gt;&gt; 1;<\/p>\n<p>x &amp;= 1;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u884c\u6ce8\u91ca<\/strong>\uff1a<\/p>\n<p>\u6bd4\u8f83\u9690\u6666\u7684\u5730\u65b9\u8981\u5728\u884c\u5c3e\u52a0\u5165\u6ce8\u91ca\uff0c\u53ef\u4ee5\u5728\u4ee3\u7801\u4e4b\u540e\u7a7a\u4e24\u683c\u52a0\u884c\u5c3e\u6ce8\u91ca\uff0c\u5982\uff1a<\/p>\n<p>\/\/ If we have enough memory, mmap the data portion too.<\/p>\n<p>mmap_budget = max&lt;int64&gt;(0, mmap_budget &#8211; index_-&gt;length());<\/p>\n<p>if (mmap_budget &gt;= data_size_ &amp;&amp; !MmapData(mmap_chunk_bytes, mlock))<\/p>\n<p>return;\u00a0 \/\/ Error already logged.<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\uff0c\u6709\u4e24\u5757\u6ce8\u91ca\u63cf\u8ff0\u8fd9\u6bb5\u4ee3\u7801\uff0c\u5f53\u51fd\u6570\u8fd4\u56de\u65f6\u6ce8\u91ca\u63d0\u53ca\u9519\u8bef\u5df2\u7ecf\u88ab\u8bb0\u5165\u65e5\u5fd7\u3002<\/p>\n<p>\u524d\u540e\u76f8\u90bb\u51e0\u884c\u90fd\u6709\u6ce8\u91ca\uff0c\u53ef\u4ee5\u9002\u5f53\u8c03\u6574\u4f7f\u4e4b\u53ef\u8bfb\u6027\u66f4\u597d\uff1a<\/p>\n<p>&#8230;<\/p>\n<p>DoSomething();\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Comment here so the comments line up.<\/p>\n<p>DoSomethingElseThatIsLonger();\u00a0 \/\/ Comment here so there are two spaces between<\/p>\n<p>\/\/ the code and the comment.<\/p>\n<p>&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p><strong>NULL<\/strong><strong>\u3001<\/strong><strong>true\/false<\/strong><strong>\u3001<\/strong><strong>1<\/strong><strong>\u3001<\/strong><strong>2<\/strong><strong>\u3001<\/strong><strong>3<\/strong><strong>\u2026\u2026<\/strong>\uff1a<\/p>\n<p>\u5411\u51fd\u6570\u4f20\u5165\u3001\u5e03\u5c14\u503c\u6216\u6574\u6570\u65f6\uff0c\u8981\u6ce8\u91ca\u8bf4\u660e\u542b\u4e49\uff0c\u6216\u4f7f\u7528\u5e38\u91cf\u8ba9\u4ee3\u7801\u671b\u6587\u77e5\u610f\uff0c\u6bd4\u8f83\u4e00\u4e0b\uff1a<\/p>\n<p>bool success = CalculateSomething(interesting_value,<\/p>\n<p>10,<\/p>\n<p>false,<\/p>\n<p>NULL);\u00a0 \/\/ What are these arguments??<\/p>\n<p>&nbsp;<\/p>\n<p>\u548c\uff1a<\/p>\n<p>bool success = CalculateSomething(interesting_value,<\/p>\n<p>10,\u00a0\u00a0\u00a0\u00a0 \/\/ Default base value.<\/p>\n<p>false,\u00a0 \/\/ Not the first time we&#8217;re calling this.<\/p>\n<p>NULL);\u00a0 \/\/ No callback.<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f7f\u7528\u5e38\u91cf\u6216\u63cf\u8ff0\u6027\u53d8\u91cf\uff1a<\/p>\n<p>const int kDefaultBaseValue = 10;<\/p>\n<p>const bool kFirstTimeCalling = false;<\/p>\n<p>Callback *null_callback = NULL;<\/p>\n<p>bool success = CalculateSomething(interesting_value,<\/p>\n<p>kDefaultBaseValue,<\/p>\n<p>kFirstTimeCalling,<\/p>\n<p>null_callback);<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u4e0d\u8981<\/strong>\uff1a<\/p>\n<p>\u6ce8\u610f\u6c38\u8fdc\u4e0d\u8981\u7528\u81ea\u7136\u8bed\u8a00\u7ffb\u8bd1\u4ee3\u7801\u4f5c\u4e3a\u6ce8\u91ca\uff0c\u8981\u5047\u8bbe\u8bfb\u4f60\u4ee3\u7801\u7684\u4ebaC++\u6bd4\u4f60\u5f3a:D\uff1a<\/p>\n<p>\/\/ Now go through the b array and make sure that if i occurs,<\/p>\n<p>\/\/ the next element is i+1.<\/p>\n<p>&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Geez.\u00a0 What a useless comment.<\/p>\n<p>&nbsp;<\/p>\n<h2>7. \u6807\u70b9\u3001\u62fc\u5199\u548c\u8bed\u6cd5\uff08Punctuation, Spelling and Grammar\uff09<\/h2>\n<p>\u7559\u610f\u6807\u70b9\u3001\u62fc\u5199\u548c\u8bed\u6cd5\uff0c\u5199\u7684\u597d\u7684\u6ce8\u91ca\u6bd4\u5dee\u7684\u8981\u6613\u8bfb\u7684\u591a\u3002<\/p>\n<p>\u6ce8\u91ca\u4e00\u822c\u662f\u5305\u542b\u9002\u5f53\u5927\u5199\u548c\u53e5\u70b9\uff08.\uff09\u7684\u5b8c\u6574\u7684\u53e5\u5b50\uff0c\u77ed\u4e00\u70b9\u7684\u6ce8\u91ca\uff08\u5982\u4ee3\u7801\u884c\u5c3e\u7684\u6ce8\u91ca\uff09\u53ef\u4ee5\u968f\u610f\u70b9\uff0c\u4f9d\u7136\u8981\u6ce8\u610f\u98ce\u683c\u7684\u4e00\u81f4\u6027\u3002\u5b8c\u6574\u7684\u53e5\u5b50\u53ef\u8bfb\u6027\u66f4\u597d\uff0c\u4e5f\u53ef\u4ee5\u8bf4\u660e\u8be5\u6ce8\u91ca\u662f\u5b8c\u6574\u7684\u800c\u4e0d\u662f\u4e00\u70b9\u4e0d\u6210\u719f\u7684\u60f3\u6cd5\u3002<\/p>\n<p>\u867d\u7136\u88ab\u522b\u4eba\u6307\u51fa\u8be5\u7528\u5206\u53f7\uff08semicolon\uff09\u7684\u65f6\u5019\u7528\u4e86\u9017\u53f7\uff08comma\uff09\u6709\u70b9\u5c34\u5c2c\u3002\u6e05\u6670\u6613\u8bfb\u7684\u4ee3\u7801\u8fd8\u662f\u5f88\u91cd\u8981\u7684\uff0c\u9002\u5f53\u7684\u6807\u70b9\u3001\u62fc\u5199\u548c\u8bed\u6cd5\u5bf9\u6b64\u4f1a\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<h2>8. TODO\u6ce8\u91ca\uff08TODO Comments\uff09<\/h2>\n<p>\u5bf9\u90a3\u4e9b\u4e34\u65f6\u7684\u3001\u77ed\u671f\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u6216\u5df2\u7ecf\u591f\u597d\u4f46\u5e76\u4e0d\u5b8c\u7f8e\u7684\u4ee3\u7801\u4f7f\u7528TODO\u6ce8\u91ca\u3002<\/p>\n<p>\u8fd9\u6837\u7684\u6ce8\u91ca\u8981\u4f7f\u7528\u5168\u5927\u5199\u7684\u5b57\u7b26\u4e32TODO\uff0c\u540e\u9762\u62ec\u53f7\uff08parentheses\uff09\u91cc\u52a0\u4e0a\u4f60\u7684\u5927\u540d\u3001\u90ae\u4ef6\u5730\u5740\u7b49\uff0c\u8fd8\u53ef\u4ee5\u52a0\u4e0a\u5192\u53f7\uff08colon\uff09\uff1a\u76ee\u7684\u662f\u53ef\u4ee5\u6839\u636e\u7edf\u4e00\u7684TODO\u683c\u5f0f\u8fdb\u884c\u67e5\u627e\uff1a<\/p>\n<p>\/\/ TODO(kl@gmail.com): Use a &#8220;*&#8221; here for concatenation operator.<\/p>\n<p>\/\/ TODO(Zeke) change this to use relations.<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u52a0\u4e0a\u662f\u4e3a\u4e86\u5728\u201c\u5c06\u6765\u67d0\u4e00\u5929\u505a\u67d0\u4e8b\u201d\uff0c\u53ef\u4ee5\u52a0\u4e0a\u4e00\u4e2a\u7279\u5b9a\u7684\u65f6\u95f4\uff08&#8221;Fix by November 2005&#8243;\uff09\u6216\u4e8b\u4ef6\uff08&#8221;Remove this code when all clients can handle XML responses.&#8221;\uff09\u3002<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u6ce8\u91ca\u4e5f\u662f\u6bd4\u8f83\u4eba\u6027\u5316\u7684\u7ea6\u5b9a\u4e86\uff1a<\/p>\n<ol>\n<li><strong>\u5173\u4e8e\u6ce8\u91ca\u98ce\u683c\uff0c\u5f88\u591a<\/strong><strong>C++<\/strong><strong>\u7684<\/strong><strong>coders<\/strong><strong>\u66f4\u559c\u6b22\u884c\u6ce8\u91ca\uff0c<\/strong><strong>C coders<\/strong><strong>\u6216\u8bb8\u5bf9\u5757\u6ce8\u91ca\u4f9d\u7136\u60c5\u6709\u72ec\u949f\uff0c\u6216\u8005\u5728\u6587\u4ef6\u5934\u5927\u6bb5\u5927\u6bb5\u7684\u6ce8\u91ca\u65f6\u4f7f\u7528\u5757\u6ce8\u91ca\uff1b<\/strong><\/li>\n<li><strong>\u6587\u4ef6\u6ce8\u91ca\u53ef\u4ee5\u70ab\u8000\u4f60\u7684\u6210\u5c31\uff0c\u4e5f\u662f\u4e3a\u4e86\u6345\u4e86\u7bd3\u5b50\u522b\u4eba\u53ef\u4ee5\u627e\u4f60\uff1b<\/strong><\/li>\n<li><strong>\u6ce8\u91ca\u8981\u8a00\u7b80\u610f\u8d45\uff0c\u4e0d\u8981\u62d6\u6c93\u5197\u4f59\uff0c\u590d\u6742\u7684\u4e1c\u897f\u7b80\u5355\u5316\u548c\u7b80\u5355\u7684\u4e1c\u897f\u590d\u6742\u5316\u90fd\u662f\u8981\u88ab\u9119\u89c6\u7684\uff1b<\/strong><\/li>\n<li><strong>\u5bf9\u4e8e<\/strong><strong>Chinese coders<\/strong><strong>\u6765\u8bf4\uff0c\u7528\u82f1\u6587\u6ce8\u91ca\u8fd8\u662f\u7528\u4e2d\u6587\u6ce8\u91ca\uff0c<\/strong><strong>it is a problem<\/strong><strong>\uff0c\u4f46\u4e0d\u7ba1\u600e\u6837\uff0c\u6ce8\u91ca\u662f\u4e3a\u4e86\u8ba9\u522b\u4eba\u770b\u61c2\uff0c\u96be\u9053\u662f\u4e3a\u4e86\u70ab\u8000\u7f16\u7a0b\u8bed\u8a00\u4e4b\u5916\u7684\u4f60\u7684\u6bcd\u8bed\u6216\u5916\u8bed\u6c34\u5e73\u5417\uff1b<\/strong><\/li>\n<li><strong>\u6ce8\u91ca\u4e0d\u8981\u592a\u4e71\uff0c\u9002\u5f53\u7684\u7f29\u8fdb\u624d\u4f1a\u8ba9\u4eba\u4e50\u610f\u770b\uff0c\u4f46\u4e5f\u6ca1\u6709\u5fc5\u8981\u89c4\u5b9a\u6ce8\u91ca\u4ece\u7b2c\u51e0\u5217\u5f00\u59cb\uff08\u6211\u81ea\u5df1\u5199\u4ee3\u7801\u7684\u65f6\u5019\u603b\u559c\u6b22\u8fd9\u6837\uff09\uff0c<\/strong><strong>UNIX\/LINUX<\/strong><strong>\u4e0b\u8fd8\u53ef\u4ee5\u7ea6\u5b9a\u662f\u4f7f\u7528<\/strong><strong>tab<\/strong><strong>\u8fd8\u662f<\/strong><strong>space<\/strong><strong>\uff0c\u4e2a\u4eba\u503e\u5411\u4e8e<\/strong><strong>space<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong> TODO<\/strong><strong>\u5f88\u4e0d\u9519\uff0c\u6709\u65f6\u5019\uff0c\u6ce8\u91ca\u786e\u5b9e\u662f\u4e3a\u4e86\u6807\u8bb0\u4e00\u4e9b\u672a\u5b8c\u6210\u7684\u6216\u5b8c\u6210\u7684\u4e0d\u5c3d\u5982\u4eba\u610f\u7684\u5730\u65b9\uff0c\u8fd9\u6837\u4e00\u641c\u7d22\uff0c\u5c31\u77e5\u9053\u8fd8\u6709\u54ea\u4e9b\u6d3b\u8981\u5e72\uff0c\u65e5\u5fd7\u90fd\u7701\u4e86\u3002<\/strong><\/li>\n<\/ol>\n<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/23\/56922.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e03\uff09<\/a><\/h1>\n<h2>\u683c\u5f0f<\/h2>\n<p>\u4ee3\u7801\u98ce\u683c\u548c\u683c\u5f0f\u786e\u5b9e\u6bd4\u8f83\u968f\u610f\uff0c\u4f46\u4e00\u4e2a\u9879\u76ee\u4e2d\u6240\u6709\u4eba\u9075\u5faa\u540c\u4e00\u98ce\u683c\u662f\u975e\u5e38\u5bb9\u6613\u7684\uff0c\u4f5c\u4e3a\u4e2a\u4eba\u672a\u5fc5\u540c\u610f\u4e0b\u8ff0\u683c\u5f0f\u89c4\u5219\u7684\u6bcf\u4e00\u5904\uff0c\u4f46\u6574\u4e2a\u9879\u76ee\u670d\u4ece\u7edf\u4e00\u7684\u7f16\u7a0b\u98ce\u683c\u662f\u5f88\u91cd\u8981\u7684\uff0c\u8fd9\u6837\u505a\u624d\u80fd\u8ba9\u6240\u6709\u4eba\u5728\u9605\u8bfb\u548c\u7406\u89e3\u4ee3\u7801\u65f6\u66f4\u52a0\u5bb9\u6613\u3002<\/p>\n<h2>1. \u884c\u957f\u5ea6\uff08Line Length\uff09<\/h2>\n<p>\u6bcf\u4e00\u884c\u4ee3\u7801\u5b57\u7b26\u6570\u4e0d\u8d85\u8fc780\u3002<\/p>\n<p>\u6211\u4eec\u4e5f\u8ba4\u8bc6\u5230\u8fd9\u6761\u89c4\u5219\u662f\u5b58\u6709\u4e89\u8bae\u7684\uff0c\u4f46\u5982\u6b64\u591a\u7684\u4ee3\u7801\u90fd\u9075\u7167\u8fd9\u4e00\u89c4\u5219\uff0c\u6211\u4eec\u611f\u89c9\u4e00\u81f4\u6027\u66f4\u91cd\u8981\u3002<\/p>\n<p><strong>\u4f18\u70b9<\/strong>\uff1a\u63d0\u5021\u8be5\u539f\u5219\u7684\u4eba\u8ba4\u4e3a\u5f3a\u8feb\u4ed6\u4eec\u8c03\u6574\u7f16\u8f91\u5668\u7a97\u53e3\u5927\u5c0f\u5f88\u91ce\u86ee\u3002\u5f88\u591a\u4eba\u540c\u65f6\u5e76\u6392\u5f00\u51e0\u4e2a\u7a97\u53e3\uff0c\u6839\u672c\u6ca1\u6709\u591a\u4f59\u7a7a\u95f4\u62d3\u5bbd\u67d0\u4e2a\u7a97\u53e3\uff0c\u4eba\u4eec\u5c06\u7a97\u53e3\u6700\u5927\u5c3a\u5bf8\u52a0\u4ee5\u9650\u5b9a\uff0c\u4e00\u81f4\u4f7f\u752880\u5217\u5bbd\uff0c\u4e3a\u4ec0\u4e48\u8981\u6539\u53d8\u5462\uff1f<\/p>\n<p><strong>\u7f3a\u70b9<\/strong>\uff1a\u53cd\u5bf9\u8be5\u539f\u5219\u7684\u4eba\u5219\u8ba4\u4e3a\u66f4\u5bbd\u7684\u4ee3\u7801\u884c\u66f4\u6613\u9605\u8bfb\uff0c80\u5217\u7684\u9650\u5236\u662f\u4e0a\u4e2a\u4e16\u7eaa60\u5e74\u4ee3\u7684\u5927\u578b\u673a\u7684\u53e4\u677f\u7f3a\u9677\uff1b\u73b0\u4ee3\u8bbe\u5907\u5177\u6709\u66f4\u5bbd\u7684\u663e\u793a\u5c4f\uff0c\u5f88\u8f7b\u677e\u7684\u53ef\u4ee5\u663e\u793a\u66f4\u591a\u4ee3\u7801\u3002<\/p>\n<p><strong>\u7ed3\u8bba<\/strong>\uff1a80\u4e2a\u5b57\u7b26\u662f\u6700\u5927\u503c\u3002\u4f8b\u5916\uff1a<\/p>\n<p>1) \u5982\u679c\u4e00\u884c\u6ce8\u91ca\u5305\u542b\u4e86\u8d85\u8fc780\u5b57\u7b26\u7684\u547d\u4ee4\u6216URL\uff0c\u51fa\u4e8e\u590d\u5236\u7c98\u8d34\u7684\u65b9\u4fbf\u53ef\u4ee5\u8d85\u8fc780\u5b57\u7b26\uff1b<\/p>\n<p>2) \u5305\u542b\u957f\u8def\u5f84\u7684\u53ef\u4ee5\u8d85\u51fa80\u5217\uff0c\u5c3d\u91cf\u907f\u514d\uff1b<\/p>\n<p>3) \u5934\u6587\u4ef6\u4fdd\u62a4\uff08\u9632\u6b62\u91cd\u590d\u5305\u542b<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/10\/55818.html\">\u7b2c\u4e00\u7bc7<\/a>\uff09\u53ef\u4ee5\u65e0\u89c6\u8be5\u539f\u5219\u3002<\/p>\n<h2>2. \u975eASCII\u5b57\u7b26\uff08Non-ASCII Characters\uff09<\/h2>\n<p>\u5c3d\u91cf\u4e0d\u4f7f\u7528\u975eASCII\u5b57\u7b26\uff0c\u4f7f\u7528\u65f6\u5fc5\u987b\u4f7f\u7528<a href=\"http:\/\/en.wikipedia.org\/wiki\/UTF-8\">UTF-8<\/a>\u683c\u5f0f\u3002<\/p>\n<p>\u54ea\u6015\u662f\u82f1\u6587\uff0c\u4e5f\u4e0d\u5e94\u5c06\u7528\u6237\u754c\u9762\u7684\u6587\u672c\u786c\u7f16\u7801\u5230\u6e90\u4ee3\u7801\u4e2d\uff0c\u56e0\u6b64\u975eASCII\u5b57\u7b26\u8981\u5c11\u7528\u3002\u7279\u6b8a\u60c5\u51b5\u4e0b\u53ef\u4ee5\u9002\u5f53\u5305\u542b\u6b64\u7c7b\u5b57\u7b26\uff0c\u5982\uff0c\u4ee3\u7801\u5206\u6790\u5916\u90e8\u6570\u636e\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u9002\u5f53\u786c\u7f16\u7801\u6570\u636e\u6587\u4ef6\u4e2d\u4f5c\u4e3a\u5206\u9694\u7b26\u7684\u975eASCII\u5b57\u7b26\u4e32\uff1b\u66f4\u5e38\u7528\u7684\u662f\uff08\u4e0d\u9700\u8981\u672c\u5730\u5316\u7684\uff09\u5355\u5143\u6d4b\u8bd5\u4ee3\u7801\u53ef\u80fd\u5305\u542b\u975eASCII\u5b57\u7b26\u4e32\u3002\u6b64\u7c7b\u60c5\u51b5\u4e0b\uff0c\u5e94\u4f7f\u7528UTF-8\u683c\u5f0f\uff0c\u56e0\u4e3a\u5f88\u591a\u5de5\u5177\u90fd\u53ef\u4ee5\u7406\u89e3\u548c\u5904\u7406\u5176\u7f16\u7801\uff0c\u5341\u516d\u8fdb\u5236\u7f16\u7801\u4e5f\u53ef\u4ee5\uff0c\u5c24\u5176\u662f\u5728\u589e\u5f3a\u53ef\u8bfb\u6027\u7684\u60c5\u51b5\u4e0b\u2014\u2014\u5982&#8221;\\xEF\\xBB\\xBF&#8221;\u662fUnicode\u7684<a href=\"http:\/\/en.wikipedia.org\/wiki\/Non-breaking_space\">zero-width no-break space<\/a>\u5b57\u7b26\uff0c\u4ee5UTF-8\u683c\u5f0f\u5305\u542b\u5728\u6e90\u6587\u4ef6\u4e2d\u662f\u4e0d\u53ef\u89c1\u7684\u3002<\/p>\n<h2>3. \u7a7a\u683c\u8fd8\u662f\u5236\u8868\u4f4d\uff08Spaces vs. Tabs\uff09<\/h2>\n<p>\u53ea\u4f7f\u7528\u7a7a\u683c\uff0c\u6bcf\u6b21\u7f29\u8fdb2\u4e2a\u7a7a\u683c\u3002<\/p>\n<p>\u4f7f\u7528\u7a7a\u683c\u8fdb\u884c\u7f29\u8fdb\uff0c\u4e0d\u8981\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528tabs\uff0c\u8bbe\u5b9a\u7f16\u8f91\u5668\u5c06tab\u8f6c\u4e3a\u7a7a\u683c\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u5728\u524d\u6bb5\u65f6\u95f4\u7684<\/strong><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/06\/06\/debian_simple_configuration.html\"><strong>\u5173\u4e8e<\/strong><strong>Debian<\/strong><strong>\u5f00\u53d1\u5b66\u4e60\u65e5\u8bb0<\/strong><\/a><strong>\u4e00\u6587\u4e2d\uff0c\u66fe\u7ed9\u51fa\u9488\u5bf9<\/strong><strong>C\/C++<\/strong><strong>\u7f16\u7801\u4f7f\u7528\u7684<\/strong><strong>vim<\/strong><strong>\u914d\u7f6e\u3002<\/strong><\/p>\n<h2>4. \u51fd\u6570\u58f0\u660e\u4e0e\u5b9a\u4e49\uff08Function Declarations and Definitions\uff09<\/h2>\n<p>\u8fd4\u56de\u7c7b\u578b\u548c\u51fd\u6570\u540d\u5728\u540c\u4e00\u884c\uff0c\u5408\u9002\u7684\u8bdd\uff0c\u53c2\u6570\u4e5f\u653e\u5728\u540c\u4e00\u884c\u3002<\/p>\n<p>\u51fd\u6570\u770b\u4e0a\u53bb\u50cf\u8fd9\u6837\uff1a<\/p>\n<p>ReturnType ClassName::FunctionName(Type par_name1, Type par_name2) {<\/p>\n<p>DoSomething();<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u540c\u4e00\u884c\u6587\u672c\u8f83\u591a\uff0c\u5bb9\u4e0d\u4e0b\u6240\u6709\u53c2\u6570\uff1a<\/p>\n<p>ReturnType ClassName::ReallyLongFunctionName(Type par_name1,<\/p>\n<p>Type par_name2,<\/p>\n<p>Type par_name3) {<\/p>\n<p>DoSomething();<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u751a\u81f3\u8fde\u7b2c\u4e00\u4e2a\u53c2\u6570\u90fd\u653e\u4e0d\u4e0b\uff1a<\/p>\n<p>ReturnType LongClassName::ReallyReallyReallyLongFunctionName(<\/p>\n<p>Type par_name1,\u00a0 \/\/ 4 space indent<\/p>\n<p>Type par_name2,<\/p>\n<p>Type par_name3) {<\/p>\n<p>DoSomething();\u00a0 \/\/ 2 space indent<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\u4ee5\u4e0b\u51e0\u70b9\uff1a<\/p>\n<p>1) \u8fd4\u56de\u503c\u603b\u662f\u548c\u51fd\u6570\u540d\u5728\u540c\u4e00\u884c\uff1b<\/p>\n<p>2) \u5de6\u5706\u62ec\u53f7\uff08open parenthesis\uff09\u603b\u662f\u548c\u51fd\u6570\u540d\u5728\u540c\u4e00\u884c\uff1b<\/p>\n<p>3) \u51fd\u6570\u540d\u548c\u5de6\u5706\u62ec\u53f7\u95f4\u6ca1\u6709\u7a7a\u683c\uff1b<\/p>\n<p>4) \u5706\u62ec\u53f7\u4e0e\u53c2\u6570\u95f4\u6ca1\u6709\u7a7a\u683c\uff1b<\/p>\n<p>5) \u5de6\u5927\u62ec\u53f7\uff08open curly brace\uff09\u603b\u5728\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u540c\u4e00\u884c\u7684\u672b\u5c3e\u5904\uff1b<\/p>\n<p>6) \u53f3\u5927\u62ec\u53f7\uff08close curly brace\uff09\u603b\u662f\u5355\u72ec\u4f4d\u4e8e\u51fd\u6570\u6700\u540e\u4e00\u884c\uff1b<\/p>\n<p>7) \u53f3\u5706\u62ec\u53f7\uff08close parenthesis\uff09\u548c\u5de6\u5927\u62ec\u53f7\u95f4\u603b\u662f\u6709\u4e00\u4e2a\u7a7a\u683c\uff1b<\/p>\n<p>8) \u51fd\u6570\u58f0\u660e\u548c\u5b9e\u73b0\u5904\u7684\u6240\u6709\u5f62\u53c2\u540d\u79f0\u5fc5\u987b\u4fdd\u6301\u4e00\u81f4\uff1b<\/p>\n<p>9) \u6240\u6709\u5f62\u53c2\u5e94\u5c3d\u53ef\u80fd\u5bf9\u9f50\uff1b<\/p>\n<p>10) \u7f3a\u7701\u7f29\u8fdb\u4e3a2\u4e2a\u7a7a\u683c\uff1b<\/p>\n<p>11) \u72ec\u7acb\u5c01\u88c5\u7684\u53c2\u6570\u4fdd\u63014\u4e2a\u7a7a\u683c\u7684\u7f29\u8fdb\u3002<\/p>\n<p>\u5982\u679c\u51fd\u6570\u4e3aconst\u7684\uff0c\u5173\u952e\u5b57const\u5e94\u4e0e\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u4f4d\u4e8e\u540c\u4e00\u884c\u3002<\/p>\n<p>\/\/ Everything in this function signature fits on a single line<\/p>\n<p>ReturnType FunctionName(Type par) const {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ This function signature requires multiple lines, but<\/p>\n<p>\/\/ the const keyword is on the line with the last parameter.<\/p>\n<p>ReturnType ReallyLongFunctionName(Type par1,<\/p>\n<p>Type par2) const {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u6709\u4e9b\u53c2\u6570\u6ca1\u6709\u7528\u5230\uff0c\u5728\u51fd\u6570\u5b9a\u4e49\u5904\u5c06\u53c2\u6570\u540d\u6ce8\u91ca\u8d77\u6765\uff1a<\/p>\n<p>\/\/ Always have named parameters in interfaces.<\/p>\n<p>class Shape {<\/p>\n<p>public:<\/p>\n<p>virtual void Rotate(double radians) = 0;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Always have named parameters in the declaration.<\/p>\n<p>class Circle : public Shape {<\/p>\n<p>public:<\/p>\n<p>virtual void Rotate(double radians);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Comment out unused named parameters in definitions.<\/p>\n<p>void Circle::Rotate(double \/*radians*\/) {}<\/p>\n<p>\/\/ Bad &#8211; if someone wants to implement later, it&#8217;s not clear what the<\/p>\n<p>\/\/ variable means.<\/p>\n<p>void Circle::Rotate(double) {}<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u5173\u4e8e<\/strong><strong>UNIX\/Linux<\/strong><strong>\u98ce\u683c\u4e3a\u4ec0\u4e48\u8981\u628a\u5de6\u5927\u62ec\u53f7\u7f6e\u4e8e\u884c\u5c3e\uff08<\/strong><strong>.cc<\/strong><strong>\u6587\u4ef6\u7684\u51fd\u6570\u5b9e\u73b0\u5904\uff0c\u5de6\u5927\u62ec\u53f7\u4f4d\u4e8e\u884c\u9996\uff09\uff0c\u6211\u7684\u7406\u89e3\u662f\u4ee3\u7801\u770b\u4e0a\u53bb\u6bd4\u8f83\u7b80\u7ea6\uff0c\u60f3\u60f3\u884c\u9996\u9664\u4e86\u51fd\u6570\u4f53\u88ab\u4e00\u5bf9\u5927\u62ec\u53f7\u5c01\u5728\u4e00\u8d77\u4e4b\u5916\uff0c\u53ea\u6709\u53f3\u5927\u62ec\u53f7\u7684\u4ee3\u7801\u770b\u4e0a\u53bb\u786e\u5b9e\u4e5f\u8212\u670d\uff1b<\/strong><strong>Windows<\/strong><strong>\u98ce\u683c\u5c06\u5de6\u5927\u62ec\u53f7\u7f6e\u4e8e\u884c\u9996\u7684\u4f18\u70b9\u662f\u5339\u914d\u60c5\u51b5\u4e00\u76ee\u4e86\u7136\u3002<\/strong><\/p>\n<h2>5. \u51fd\u6570\u8c03\u7528\uff08Function Calls\uff09<\/h2>\n<p>\u5c3d\u91cf\u653e\u5728\u540c\u4e00\u884c\uff0c\u5426\u5219\uff0c\u5c06\u5b9e\u53c2\u5c01\u88c5\u5728\u5706\u62ec\u53f7\u4e2d\u3002<\/p>\n<p>\u51fd\u6570\u8c03\u7528\u9075\u5faa\u5982\u4e0b\u5f62\u5f0f\uff1a<\/p>\n<p>bool retval = DoSomething(argument1, argument2, argument3);<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u540c\u4e00\u884c\u653e\u4e0d\u4e0b\uff0c\u53ef\u65ad\u4e3a\u591a\u884c\uff0c\u540e\u9762\u6bcf\u4e00\u884c\u90fd\u548c\u7b2c\u4e00\u4e2a\u5b9e\u53c2\u5bf9\u9f50\uff0c\u5de6\u5706\u62ec\u53f7\u540e\u548c\u53f3\u5706\u62ec\u53f7\u524d\u4e0d\u8981\u7559\u7a7a\u683c\uff1a<\/p>\n<p>bool retval = DoSomething(averyveryveryverylongargument1,<\/p>\n<p>argument2, argument3);<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u51fd\u6570\u53c2\u6570\u6bd4\u8f83\u591a\uff0c\u53ef\u4ee5\u51fa\u4e8e\u53ef\u8bfb\u6027\u7684\u8003\u8651\u6bcf\u884c\u53ea\u653e\u4e00\u4e2a\u53c2\u6570\uff1a<\/p>\n<p>bool retval = DoSomething(argument1,<\/p>\n<p>argument2,<\/p>\n<p>argument3,<\/p>\n<p>argument4);<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u51fd\u6570\u540d\u592a\u957f\uff0c\u4ee5\u81f3\u4e8e\u8d85\u8fc7\u884c\u6700\u5927\u957f\u5ea6\uff0c\u53ef\u4ee5\u5c06\u6240\u6709\u53c2\u6570\u72ec\u7acb\u6210\u884c\uff1a<\/p>\n<p>if (&#8230;) {<\/p>\n<p>&#8230;<\/p>\n<p>&#8230;<\/p>\n<p>if (&#8230;) {<\/p>\n<p>DoSomethingThatRequiresALongFunctionName(<\/p>\n<p>very_long_argument1,\u00a0 \/\/ 4 space indent<\/p>\n<p>argument2,<\/p>\n<p>argument3,<\/p>\n<p>argument4);<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2>6. \u6761\u4ef6\u8bed\u53e5\uff08Conditionals\uff09<\/h2>\n<p>\u66f4\u63d0\u5021\u4e0d\u5728\u5706\u62ec\u53f7\u4e2d\u6dfb\u52a0\u7a7a\u683c\uff0c\u5173\u952e\u5b57else\u53e6\u8d77\u4e00\u884c\u3002<\/p>\n<p>\u5bf9\u57fa\u672c\u6761\u4ef6\u8bed\u53e5\u6709\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u683c\u5f0f\uff0c\u4e00\u79cd\u5728\u5706\u62ec\u53f7\u548c\u6761\u4ef6\u4e4b\u95f4\u6709\u7a7a\u683c\uff0c\u4e00\u79cd\u6ca1\u6709\u3002<\/p>\n<p>\u6700\u5e38\u89c1\u7684\u662f\u6ca1\u6709\u7a7a\u683c\u7684\u683c\u5f0f\uff0c\u90a3\u79cd\u90fd\u53ef\u4ee5\uff0c\u8fd8\u662f<strong>\u4e00\u81f4\u6027<\/strong>\u4e3a\u4e3b\u3002\u5982\u679c\u4f60\u662f\u5728\u4fee\u6539\u4e00\u4e2a\u6587\u4ef6\uff0c\u53c2\u8003\u5f53\u524d\u5df2\u6709\u683c\u5f0f\uff1b\u5982\u679c\u662f\u5199\u65b0\u7684\u4ee3\u7801\uff0c\u53c2\u8003\u76ee\u5f55\u4e0b\u6216\u9879\u76ee\u4e2d\u5176\u4ed6\u6587\u4ef6\u7684\u683c\u5f0f\uff0c\u8fd8\u5728\u5f98\u5f8a\u7684\u8bdd\uff0c\u5c31\u4e0d\u8981\u52a0\u7a7a\u683c\u4e86\u3002<\/p>\n<p>if (condition) {\u00a0 \/\/ no spaces inside parentheses<\/p>\n<p>&#8230;\u00a0 \/\/ 2 space indent.<\/p>\n<p>} else {\u00a0 \/\/ The else goes on the same line as the closing brace.<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u4f60\u503e\u5411\u4e8e\u5728\u5706\u62ec\u53f7\u5185\u90e8\u52a0\u7a7a\u683c\uff1a<\/p>\n<p>if ( condition ) {\u00a0 \/\/ spaces inside parentheses &#8211; rare<\/p>\n<p>&#8230;\u00a0 \/\/ 2 space indent.<\/p>\n<p>} else {\u00a0 \/\/ The else goes on the same line as the closing brace.<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\u6240\u6709\u60c5\u51b5\u4e0bif\u548c\u5de6\u5706\u62ec\u53f7\u95f4\u6709\u4e2a\u7a7a\u683c\uff0c\u53f3\u5706\u62ec\u53f7\u548c\u5de6\u5927\u62ec\u53f7\uff08\u5982\u679c\u4f7f\u7528\u7684\u8bdd\uff09\u95f4\u4e5f\u8981\u6709\u4e2a\u7a7a\u683c\uff1a<\/p>\n<p>if(condition)\u00a0\u00a0\u00a0\u00a0 \/\/ Bad &#8211; space missing after IF.<\/p>\n<p>if (condition){\u00a0\u00a0 \/\/ Bad &#8211; space missing before {.<\/p>\n<p>if(condition){\u00a0\u00a0\u00a0 \/\/ Doubly bad.<\/p>\n<p>if (condition) {\u00a0 \/\/ Good &#8211; proper space after IF and before {.<\/p>\n<p>&nbsp;<\/p>\n<p>\u6709\u4e9b\u6761\u4ef6\u8bed\u53e5\u5199\u5728\u540c\u4e00\u884c\u4ee5\u589e\u5f3a\u53ef\u8bfb\u6027\uff0c\u53ea\u6709\u5f53\u8bed\u53e5\u7b80\u5355\u5e76\u4e14\u6ca1\u6709\u4f7f\u7528else\u5b50\u53e5\u65f6\u4f7f\u7528\uff1a<\/p>\n<p>if (x == kFoo) return new Foo();<\/p>\n<p>if (x == kBar) return new Bar();<\/p>\n<p>&nbsp;<\/p>\n<p>\u5982\u679c\u8bed\u53e5\u6709else\u5206\u652f\u662f\u4e0d\u5141\u8bb8\u7684\uff1a<\/p>\n<p>\/\/ Not allowed &#8211; IF statement on one line when there is an ELSE clause<\/p>\n<p>if (x) DoThis();<\/p>\n<p>else DoThat();<\/p>\n<p>&nbsp;<\/p>\n<p>\u901a\u5e38\uff0c\u5355\u884c\u8bed\u53e5\u4e0d\u9700\u8981\u4f7f\u7528\u5927\u62ec\u53f7\uff0c\u5982\u679c\u4f60\u559c\u6b22\u4e5f\u65e0\u53ef\u539a\u975e\uff0c\u4e5f\u6709\u4eba\u8981\u6c42if\u5fc5\u987b\u4f7f\u7528\u5927\u62ec\u53f7\uff1a<\/p>\n<p>if (condition)<\/p>\n<p>DoSomething();\u00a0 \/\/ 2 space indent.<\/p>\n<p>&nbsp;<\/p>\n<p>if (condition) {<\/p>\n<p>DoSomething();\u00a0 \/\/ 2 space indent.<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u4f46\u5982\u679c\u8bed\u53e5\u4e2d\u54ea\u4e00\u5206\u652f\u4f7f\u7528\u4e86\u5927\u62ec\u53f7\u7684\u8bdd\uff0c\u5176\u4ed6\u90e8\u5206\u4e5f\u5fc5\u987b\u4f7f\u7528\uff1a<\/p>\n<p>\/\/ Not allowed &#8211; curly on IF but not ELSE<\/p>\n<p>if (condition) {<\/p>\n<p>foo;<\/p>\n<p>} else<\/p>\n<p>bar;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Not allowed &#8211; curly on ELSE but not IF<\/p>\n<p>if (condition)<\/p>\n<p>foo;<\/p>\n<p>else {<\/p>\n<p>bar;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Curly braces around both IF and ELSE required because<\/p>\n<p>\/\/ one of the clauses used braces.<\/p>\n<p>if (condition) {<\/p>\n<p>foo;<\/p>\n<p>} else {<\/p>\n<p>bar;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2>7. \u5faa\u73af\u548c\u5f00\u5173\u9009\u62e9\u8bed\u53e5\uff08Loops and Switch Statements\uff09<\/h2>\n<p>switch\u8bed\u53e5\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7\u5206\u5757\uff1b\u7a7a\u5faa\u73af\u4f53\u5e94\u4f7f\u7528{}\u6216continue\u3002<\/p>\n<p>switch\u8bed\u53e5\u4e2d\u7684case\u5757\u53ef\u4ee5\u4f7f\u7528\u5927\u62ec\u53f7\u4e5f\u53ef\u4ee5\u4e0d\u7528\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684\u559c\u597d\uff0c\u4f7f\u7528\u65f6\u8981\u4f9d\u4e0b\u6587\u6240\u8ff0\u3002<\/p>\n<p>\u5982\u679c\u6709\u4e0d\u6ee1\u8db3case\u679a\u4e3e\u6761\u4ef6\u7684\u503c\uff0c\u8981\u603b\u662f\u5305\u542b\u4e00\u4e2adefault\uff08\u5982\u679c\u6709\u8f93\u5165\u503c\u6ca1\u6709case\u53bb\u5904\u7406\uff0c\u7f16\u8bd1\u5668\u5c06\u62a5\u8b66\uff09\u3002\u5982\u679cdefault\u6c38\u4e0d\u4f1a\u6267\u884c\uff0c\u53ef\u4ee5\u7b80\u5355\u7684\u4f7f\u7528assert\uff1a<\/p>\n<p>switch (var) {<\/p>\n<p>case 0: {\u00a0 \/\/ 2 space indent<\/p>\n<p>&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 4 space indent<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>case 1: {<\/p>\n<p>&#8230;<\/p>\n<p>break;<\/p>\n<p>}<\/p>\n<p>default: {<\/p>\n<p>assert(false);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u7a7a\u5faa\u73af\u4f53\u5e94\u4f7f\u7528{}\u6216continue\uff0c\u800c\u4e0d\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u5206\u53f7\uff1a<\/p>\n<p>while (condition) {<\/p>\n<p>\/\/ Repeat test until it returns false.<\/p>\n<p>}<\/p>\n<p>for (int i = 0; i &lt; kSomeNumber; ++i) {}\u00a0 \/\/ Good &#8211; empty body.<\/p>\n<p>while (condition) continue;\u00a0 \/\/ Good &#8211; continue indicates no logic.<\/p>\n<p>while (condition);\u00a0 \/\/ Bad &#8211; looks like part of do\/while loop.<\/p>\n<p>&nbsp;<\/p>\n<h2>8. \u6307\u9488\u548c\u5f15\u7528\u8868\u8fbe\u5f0f\uff08Pointers and Reference Expressions\uff09<\/h2>\n<p>\u53e5\u70b9\uff08.\uff09\u6216\u7bad\u5934\uff08-&gt;\uff09\u524d\u540e\u4e0d\u8981\u6709\u7a7a\u683c\uff0c\u6307\u9488\/\u5730\u5740\u64cd\u4f5c\u7b26\uff08*\u3001&amp;\uff09\u540e\u4e0d\u8981\u6709\u7a7a\u683c\u3002<\/p>\n<p>\u4e0b\u9762\u662f\u6307\u9488\u548c\u5f15\u7528\u8868\u8fbe\u5f0f\u7684\u6b63\u786e\u8303\u4f8b\uff1a<\/p>\n<p>x = *p;<\/p>\n<p>p = &amp;x;<\/p>\n<p>x = r.y;<\/p>\n<p>x = r-&gt;y;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1) \u5728\u8bbf\u95ee\u6210\u5458\u65f6\uff0c\u53e5\u70b9\u6216\u7bad\u5934\u524d\u540e\u6ca1\u6709\u7a7a\u683c\uff1b<\/p>\n<p>2) \u6307\u9488\u64cd\u4f5c\u7b26*\u6216&amp;\u540e\u6ca1\u6709\u7a7a\u683c\u3002<\/p>\n<p>\u5728\u58f0\u660e\u6307\u9488\u53d8\u91cf\u6216\u53c2\u6570\u65f6\uff0c\u661f\u53f7\u4e0e\u7c7b\u578b\u6216\u53d8\u91cf\u540d\u7d27\u6328\u90fd\u53ef\u4ee5\uff1a<\/p>\n<p>\/\/ These are fine, space preceding.<\/p>\n<p>char *c;<\/p>\n<p>const string &amp;str;<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ These are fine, space following.<\/p>\n<p>char* c;\u00a0\u00a0\u00a0 \/\/ but remember to do &#8220;char* c, *d, *e, &#8230;;&#8221;!<\/p>\n<p>const string&amp; str;<\/p>\n<p>char * c;\u00a0 \/\/ Bad &#8211; spaces on both sides of *<\/p>\n<p>const string &amp; str;\u00a0 \/\/ Bad &#8211; spaces on both sides of &amp;<\/p>\n<p>&nbsp;<\/p>\n<p>\u540c\u4e00\u4e2a\u6587\u4ef6\uff08\u65b0\u5efa\u6216\u73b0\u6709\uff09\u4e2d\u8d77\u7801\u8981\u4fdd\u6301\u4e00\u81f4\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u4e2a\u4eba\u6bd4\u8f83\u4e60\u60ef\u4e0e\u53d8\u91cf\u7d27\u6328\u7684\u65b9\u5f0f\u3002<\/strong><\/p>\n<h2>9. \u5e03\u5c14\u8868\u8fbe\u5f0f\uff08Boolean Expressions\uff09<\/h2>\n<p>\u5982\u679c\u4e00\u4e2a\u5e03\u5c14\u8868\u8fbe\u5f0f\u8d85\u8fc7\u6807\u51c6\u884c\u5bbd\uff0880\u5b57\u7b26\uff09\uff0c\u5982\u679c\u65ad\u884c\u8981\u7edf\u4e00\u4e00\u4e0b\u3002<\/p>\n<p>\u4e0b\u4f8b\u4e2d\uff0c\u903b\u8f91\u4e0e\uff08&amp;&amp;\uff09\u64cd\u4f5c\u7b26\u603b\u4f4d\u4e8e\u884c\u5c3e\uff1a<\/p>\n<p>if (this_one_thing &gt; this_other_thing &amp;&amp;<\/p>\n<p>a_third_thing == a_fourth_thing &amp;&amp;<\/p>\n<p>yet_another &amp; last_one) {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e24\u4e2a\u903b\u8f91\u4e0e\uff08&amp;&amp;\uff09\u64cd\u4f5c\u7b26\u90fd\u4f4d\u4e8e\u884c\u5c3e\uff0c\u53ef\u4ee5\u8003\u8651\u989d\u5916\u63d2\u5165\u5706\u62ec\u53f7\uff0c\u5408\u7406\u4f7f\u7528\u7684\u8bdd\u5bf9\u589e\u5f3a\u53ef\u8bfb\u6027\u662f\u5f88\u6709\u5e2e\u52a9\u7684\u3002<\/p>\n<p><strong>\u8bd1\u8005\u6ce8\uff1a\u4e2a\u4eba\u6bd4\u8f83\u4e60\u60ef\u903b\u8f91\u8fd0\u7b97\u7b26\u4f4d\u4e8e\u884c\u9996\uff0c\u903b\u8f91\u5173\u7cfb\u4e00\u76ee\u4e86\u7136\uff0c\u5404\u4eba\u559c\u597d\u800c\u5df2\uff0c\u81f3\u4e8e\u52a0\u4e0d\u52a0\u5706\u62ec\u53f7\u7684\u95ee\u9898\uff0c\u5982\u679c\u4f60\u5bf9\u4f18\u5148\u7ea7\u4e86\u7136\u4e8e\u80f8\u7684\u8bdd\u53ef\u4ee5\u4e0d\u52a0\uff0c\u4f46\u53ef\u8bfb\u6027\u603b\u662f\u5dee\u4e86\u4e9b\u3002<\/strong><\/p>\n<h2>10. \u51fd\u6570\u8fd4\u56de\u503c\uff08Return Values\uff09<\/h2>\n<p>return\u8868\u8fbe\u5f0f\u4e2d\u4e0d\u8981\u4f7f\u7528\u5706\u62ec\u53f7\u3002<\/p>\n<p>\u51fd\u6570\u8fd4\u56de\u65f6\u4e0d\u8981\u4f7f\u7528\u5706\u62ec\u53f7\uff1a<\/p>\n<p>return x;\u00a0 \/\/ not return(x);<\/p>\n<p>&nbsp;<\/p>\n<h2>11. \u53d8\u91cf\u53ca\u6570\u7ec4\u521d\u59cb\u5316\uff08Variable and Array Initialization\uff09<\/h2>\n<p>\u9009\u62e9=\u8fd8\u662f()\u3002<\/p>\n<p>\u9700\u8981\u505a\u4e8c\u8005\u4e4b\u95f4\u505a\u51fa\u9009\u62e9\uff0c\u4e0b\u9762\u7684\u5f62\u5f0f\u90fd\u662f\u6b63\u786e\u7684\uff1a<\/p>\n<p>int x = 3;<\/p>\n<p>int x(3);<\/p>\n<p>string name(&#8220;Some Name&#8221;);<\/p>\n<p>string name = &#8220;Some Name&#8221;;<\/p>\n<p>&nbsp;<\/p>\n<h2>12. \u9884\u5904\u7406\u6307\u4ee4\uff08Preprocessor Directives\uff09<\/h2>\n<p>\u9884\u5904\u7406\u6307\u4ee4\u4e0d\u8981\u7f29\u8fdb\uff0c\u4ece\u884c\u9996\u5f00\u59cb\u3002<\/p>\n<p>\u5373\u4f7f\u9884\u5904\u7406\u6307\u4ee4\u4f4d\u4e8e\u7f29\u8fdb\u4ee3\u7801\u5757\u4e2d\uff0c\u6307\u4ee4\u4e5f\u5e94\u4ece\u884c\u9996\u5f00\u59cb\u3002<\/p>\n<p>\/\/ Good &#8211; directives at beginning of line<\/p>\n<p>if (lopsided_score) {<\/p>\n<p>#if DISASTER_PENDING\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Correct &#8212; Starts at beginning of line<\/p>\n<p>DropEverything();<\/p>\n<p>#endif<\/p>\n<p>BackToNormal();<\/p>\n<p>}<\/p>\n<p>\/\/ Bad &#8211; indented directives<\/p>\n<p>if (lopsided_score) {<\/p>\n<p>#if DISASTER_PENDING\u00a0 \/\/ Wrong!\u00a0 The &#8220;#if&#8221; should be at beginning of line<\/p>\n<p>DropEverything();<\/p>\n<p>#endif\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Wrong!\u00a0 Do not indent &#8220;#endif&#8221;<\/p>\n<p>BackToNormal();<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2>13. \u7c7b\u683c\u5f0f\uff08Class Format\uff09<\/h2>\n<p>\u58f0\u660e\u5c5e\u6027\u4f9d\u6b21\u5e8f\u662fpublic:\u3001protected:\u3001private:\uff0c\u6bcf\u6b21\u7f29\u8fdb1\u4e2a\u7a7a\u683c\uff08\u8bd1\u8005\u6ce8\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u662f\u4e24\u4e2a\u5462\uff1f\u4e5f\u6709\u4eba\u63d0\u5021private\u5728\u524d\uff0c\u5bf9\u4e8e\u58f0\u660e\u4e86\u54ea\u4e9b\u6570\u636e\u6210\u5458\u4e00\u76ee\u4e86\u7136\uff0c\u8fd8\u6709\u4eba\u63d0\u5021\u4f9d\u903b\u8f91\u5173\u7cfb\u5c06\u53d8\u91cf\u4e0e\u64cd\u4f5c\u653e\u5728\u4e00\u8d77\uff0c\u90fd\u6709\u9053\u7406:-)\uff09\u3002<\/p>\n<p>\u7c7b\u58f0\u660e\uff08\u5bf9\u7c7b\u6ce8\u91ca\u4e0d\u4e86\u89e3\u7684\u8bdd\uff0c\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/22\/56873.html\">\u7b2c\u516d\u7bc7<\/a>\u4e2d\u7684<strong>\u7c7b\u6ce8\u91ca<\/strong>\u4e00\u8282\uff09\u7684\u57fa\u672c\u683c\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>class MyClass : public OtherClass {<\/p>\n<p>public:\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Note the 1 space indent!<\/p>\n<p>MyClass();\u00a0 \/\/ Regular 2 space indent.<\/p>\n<p>explicit MyClass(int var);<\/p>\n<p>~MyClass() {}<\/p>\n<p>&nbsp;<\/p>\n<p>void SomeFunction();<\/p>\n<p>void SomeFunctionThatDoesNothing() {<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>void set_some_var(int var) { some_var_ = var; }<\/p>\n<p>int some_var() const { return some_var_; }<\/p>\n<p>&nbsp;<\/p>\n<p>private:<\/p>\n<p>bool SomeInternalFunction();<\/p>\n<p>&nbsp;<\/p>\n<p>int some_var_;<\/p>\n<p>int some_other_var_;<\/p>\n<p>DISALLOW_COPY_AND_ASSIGN(MyClass);<\/p>\n<p>};<\/p>\n<p>&nbsp;<\/p>\n<p>\u6ce8\u610f\uff1a<\/p>\n<p>1) \u6240\u4ee5\u57fa\u7c7b\u540d\u5e94\u572880\u5217\u9650\u5236\u4e0b\u5c3d\u91cf\u4e0e\u5b50\u7c7b\u540d\u653e\u5728\u540c\u4e00\u884c\uff1b<\/p>\n<p>2) \u5173\u952e\u8bcdpublic:\u3001protected:\u3001private:\u8981\u7f29\u8fdb1\u4e2a\u7a7a\u683c\uff08\u8bd1\u8005\u6ce8\uff0cMSVC\u591a\u4f7f\u7528tab\u7f29\u8fdb\uff0c\u4e14\u8fd9\u4e09\u4e2a\u5173\u952e\u8bcd\u6ca1\u6709\u7f29\u8fdb\uff09\uff1b<\/p>\n<p>3) \u9664\u7b2c\u4e00\u4e2a\u5173\u952e\u8bcd\uff08\u4e00\u822c\u662fpublic\uff09\u5916\uff0c\u5176\u4ed6\u5173\u952e\u8bcd\u524d\u7a7a\u4e00\u884c\uff0c\u5982\u679c\u7c7b\u6bd4\u8f83\u5c0f\u7684\u8bdd\u4e5f\u53ef\u4ee5\u4e0d\u7a7a\uff1b<\/p>\n<p>4) \u8fd9\u4e9b\u5173\u952e\u8bcd\u540e\u4e0d\u8981\u7a7a\u884c\uff1b<\/p>\n<p>5) public\u653e\u5728\u6700\u524d\u9762\uff0c\u7136\u540e\u662fprotected\u548cprivate\uff1b<\/p>\n<p>6) \u5173\u4e8e\u58f0\u660e\u6b21\u5e8f\u53c2\u8003<a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/16\/56324.html\">\u7b2c\u4e09\u7bc7<\/a><strong>\u58f0\u660e\u6b21\u5e8f<\/strong>\u4e00\u8282\u3002<\/p>\n<h2>14. \u521d\u59cb\u5316\u5217\u8868\uff08Initializer Lists\uff09<\/h2>\n<p>\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u5217\u8868\u653e\u5728\u540c\u4e00\u884c\u6216\u6309\u56db\u683c\u7f29\u8fdb\u5e76\u6392\u51e0\u884c\u3002<\/p>\n<p>\u4e24\u79cd\u53ef\u4ee5\u63a5\u53d7\u7684\u521d\u59cb\u5316\u5217\u8868\u683c\u5f0f\uff1a<\/p>\n<p>\/\/ When it all fits on one line:<\/p>\n<p>MyClass::MyClass(int var) : some_var_(var), some_other_var_(var + 1) {<\/p>\n<p>&nbsp;<\/p>\n<p>\u6216<\/p>\n<p>\/\/ When it requires multiple lines, indent 4 spaces, putting the colon on<\/p>\n<p>\/\/ the first initializer line:<\/p>\n<p>MyClass::MyClass(int var)<\/p>\n<p>: some_var_(var),\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ 4 space indent<\/p>\n<p>some_other_var_(var + 1) {\u00a0 \/\/ lined up<\/p>\n<p>&#8230;<\/p>\n<p>DoSomething();<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<h2>15. \u547d\u540d\u7a7a\u95f4\u683c\u5f0f\u5316\uff08Namespace Formatting\uff09<\/h2>\n<p>\u547d\u540d\u7a7a\u95f4\u5185\u5bb9\u4e0d\u7f29\u8fdb\u3002<\/p>\n<p>\u547d\u540d\u7a7a\u95f4\u4e0d\u6dfb\u52a0\u989d\u5916\u7f29\u8fdb\u5c42\u6b21\uff0c\u4f8b\u5982\uff1a<\/p>\n<p>namespace {<\/p>\n<p>&nbsp;<\/p>\n<p>void foo() {\u00a0 \/\/ Correct.\u00a0 No extra indentation within namespace.<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}\u00a0 \/\/ namespace<\/p>\n<p>&nbsp;<\/p>\n<p>\u4e0d\u8981\u7f29\u8fdb\uff1a<\/p>\n<p>namespace {<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Wrong.\u00a0 Indented when it should not be.<\/p>\n<p>void foo() {<\/p>\n<p>&#8230;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>}\u00a0 \/\/ namespace<\/p>\n<p>&nbsp;<\/p>\n<h2>16. \u6c34\u5e73\u7559\u767d\uff08Horizontal Whitespace\uff09<\/h2>\n<p>\u6c34\u5e73\u7559\u767d\u7684\u4f7f\u7528\u56e0\u5730\u5236\u5b9c\u3002\u4e0d\u8981\u5728\u884c\u5c3e\u6dfb\u52a0\u65e0\u8c13\u7684\u7559\u767d\u3002<\/p>\n<p><strong>\u666e\u901a<\/strong>\uff1a<\/p>\n<p>void f(bool b) {\u00a0 \/\/ Open braces should always have a space before them.<\/p>\n<p>&#8230;<\/p>\n<p>int i = 0;\u00a0 \/\/ Semicolons usually have no space before them.<\/p>\n<p>int x[] = { 0 };\u00a0 \/\/ Spaces inside braces for array initialization are<\/p>\n<p>int x[] = {0};\u00a0\u00a0\u00a0 \/\/ optional.\u00a0 If you use them, put them on both sides!<\/p>\n<p>\/\/ Spaces around the colon in inheritance and initializer lists.<\/p>\n<p>class Foo : public Bar {<\/p>\n<p>public:<\/p>\n<p>\/\/ For inline function implementations, put spaces between the braces<\/p>\n<p>\/\/ and the implementation itself.<\/p>\n<p>Foo(int b) : Bar(), baz_(b) {}\u00a0 \/\/ No spaces inside empty braces.<\/p>\n<p>void Reset() { baz_ = 0; }\u00a0 \/\/ Spaces separating braces from implementation.<\/p>\n<p>&#8230;<\/p>\n<p>&nbsp;<\/p>\n<p>\u6dfb\u52a0\u5197\u4f59\u7684\u7559\u767d\u4f1a\u7ed9\u5176\u4ed6\u4eba\u7f16\u8f91\u65f6\u9020\u6210\u989d\u5916\u8d1f\u62c5\uff0c\u56e0\u6b64\uff0c\u4e0d\u8981\u52a0\u5165\u591a\u4f59\u7684\u7a7a\u683c\u3002\u5982\u679c\u786e\u5b9a\u4e00\u884c\u4ee3\u7801\u5df2\u7ecf\u4fee\u6539\u5b8c\u6bd5\uff0c\u5c06\u591a\u4f59\u7684\u7a7a\u683c\u53bb\u6389\uff1b\u6216\u8005\u5728\u4e13\u95e8\u6e05\u7406\u7a7a\u683c\u65f6\u53bb\u6389\uff08\u786e\u4fe1\u6ca1\u6709\u5176\u4ed6\u4eba\u5728\u4f7f\u7528\uff09\u3002<\/p>\n<p><strong>\u5faa\u73af\u548c\u6761\u4ef6\u8bed\u53e5<\/strong>\uff1a<\/p>\n<p>if (b) {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Space after the keyword in conditions and loops.<\/p>\n<p>} else {\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Spaces around else.<\/p>\n<p>}<\/p>\n<p>while (test) {}\u00a0\u00a0 \/\/ There is usually no space inside parentheses.<\/p>\n<p>switch (i) {<\/p>\n<p>for (int i = 0; i &lt; 5; ++i) {<\/p>\n<p>switch ( i ) {\u00a0\u00a0\u00a0 \/\/ Loops and conditions may have spaces inside<\/p>\n<p>if ( test ) {\u00a0\u00a0\u00a0\u00a0 \/\/ parentheses, but this is rare.\u00a0 Be consistent.<\/p>\n<p>for ( int i = 0; i &lt; 5; ++i ) {<\/p>\n<p>for ( ; i &lt; 5 ; ++i) {\u00a0 \/\/ For loops always have a space after the<\/p>\n<p>&#8230;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ semicolon, and may have a space before the<\/p>\n<p>\/\/ semicolon.<\/p>\n<p>switch (i) {<\/p>\n<p>case 1:\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ No space before colon in a switch case.<\/p>\n<p>&#8230;<\/p>\n<p>case 2: break;\u00a0 \/\/ Use a space after a colon if there&#8217;s code after it.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u64cd\u4f5c\u7b26<\/strong>\uff1a<\/p>\n<p>x = 0;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Assignment operators always have spaces around<\/p>\n<p>\/\/ them.<\/p>\n<p>x = -5;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ No spaces separating unary operators and their<\/p>\n<p>++x;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ arguments.<\/p>\n<p>if (x &amp;&amp; !y)<\/p>\n<p>&#8230;<\/p>\n<p>v = w * x + y \/ z;\u00a0 \/\/ Binary operators usually have spaces around them,<\/p>\n<p>v = w*x + y\/z;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ but it&#8217;s okay to remove spaces around factors.<\/p>\n<p>v = w * (x + z);\u00a0\u00a0\u00a0 \/\/ Parentheses should have no spaces inside them.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>\u6a21\u677f\u548c\u8f6c\u6362<\/strong>\uff1a<\/p>\n<p>vector&lt;string&gt; x;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ No spaces inside the angle<\/p>\n<p>y = static_cast&lt;char*&gt;(x);\u00a0 \/\/ brackets (&lt; and &gt;), before<\/p>\n<p>\/\/ &lt;, or between &gt;( in a cast.<\/p>\n<p>vector&lt;char *&gt; x;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Spaces between type and pointer are<\/p>\n<p>\/\/ okay, but be consistent.<\/p>\n<p>set&lt;list&lt;string&gt; &gt; x;\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ C++ requires a space in &gt; &gt;.<\/p>\n<p>set&lt; list&lt;string&gt; &gt; x;\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ You may optionally make use<\/p>\n<p>\/\/ symmetric spacing in &lt; &lt;.<\/p>\n<p>&nbsp;<\/p>\n<h2>17. \u5782\u76f4\u7559\u767d\uff08Vertical Whitespace\uff09<\/h2>\n<p>\u5782\u76f4\u7559\u767d\u8d8a\u5c11\u8d8a\u597d\u3002<\/p>\n<p>\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u89c4\u5219\u800c\u662f\u539f\u5219\u95ee\u9898\u4e86\uff1a\u4e0d\u662f\u975e\u5e38\u6709\u5fc5\u8981\u7684\u8bdd\u5c31\u4e0d\u8981\u4f7f\u7528\u7a7a\u884c\u3002\u5c24\u5176\u662f\uff1a\u4e0d\u8981\u5728\u4e24\u4e2a\u51fd\u6570\u5b9a\u4e49\u4e4b\u95f4\u7a7a\u8d85\u8fc72\u884c\uff0c\u51fd\u6570\u4f53\u5934\u3001\u5c3e\u4e0d\u8981\u6709\u7a7a\u884c\uff0c\u51fd\u6570\u4f53\u4e2d\u4e5f\u4e0d\u8981\u968f\u610f\u6dfb\u52a0\u7a7a\u884c\u3002<\/p>\n<p>\u57fa\u672c\u539f\u5219\u662f\uff1a\u540c\u4e00\u5c4f\u53ef\u4ee5\u663e\u793a\u8d8a\u591a\u7684\u4ee3\u7801\uff0c\u7a0b\u5e8f\u7684\u63a7\u5236\u6d41\u5c31\u8d8a\u5bb9\u6613\u7406\u89e3\u3002\u5f53\u7136\uff0c\u8fc7\u4e8e\u5bc6\u96c6\u7684\u4ee3\u7801\u5757\u548c\u8fc7\u4e8e\u758f\u677e\u7684\u4ee3\u7801\u5757\u540c\u6837\u96be\u770b\uff0c\u53d6\u51b3\u4e8e\u4f60\u7684\u5224\u65ad\uff0c\u4f46\u901a\u5e38\u662f\u8d8a\u5c11\u8d8a\u597d\u3002<\/p>\n<p>\u51fd\u6570\u5934\u3001\u5c3e\u4e0d\u8981\u6709\u7a7a\u884c\uff1a<\/p>\n<p>void Function() {<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Unnecessary blank lines before and after<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>\u4ee3\u7801\u5757\u5934\u3001\u5c3e\u4e0d\u8981\u6709\u7a7a\u884c\uff1a<\/p>\n<p>while (condition) {<\/p>\n<p>\/\/ Unnecessary blank line after<\/p>\n<p>&nbsp;<\/p>\n<p>}<\/p>\n<p>if (condition) {<\/p>\n<p>&nbsp;<\/p>\n<p>\/\/ Unnecessary blank line before<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>if-else\u5757\u4e4b\u95f4\u7a7a\u4e00\u884c\u8fd8\u53ef\u4ee5\u63a5\u53d7\uff1a<\/p>\n<p>if (condition) {<\/p>\n<p>\/\/ Some lines of code too small to move to another function,<\/p>\n<p>\/\/ followed by a blank line.<\/p>\n<p>&nbsp;<\/p>\n<p>} else {<\/p>\n<p>\/\/ Another block of code<\/p>\n<p>}<\/p>\n<p>&nbsp;<\/p>\n<p>______________________________________<\/p>\n<p>\u8bd1\u8005\uff1a\u9996\u5148\u8bf4\u660e\uff0c\u5bf9\u4e8e\u4ee3\u7801\u683c\u5f0f\uff0c\u56e0\u4eba\u3001\u56e0\u7cfb\u7edf\u5404\u6709\u4f18\u7f3a\u70b9\uff0c\u4f46\u540c\u4e00\u4e2a\u9879\u76ee\u4e2d\u9075\u5faa\u540c\u4e00\u6807\u51c6\u8fd8\u662f\u6709\u5fc5\u8981\u7684\uff1a<\/p>\n<ol>\n<li><strong>\u884c\u5bbd\u539f\u5219\u4e0a\u4e0d\u8d85\u8fc7<\/strong><strong>80<\/strong><strong>\u5217\uff0c\u628a<\/strong><strong>22<\/strong><strong>\u5bf8\u7684\u663e\u793a\u5c4f\u90fd\u5360\u5b8c\uff0c\u600e\u4e48\u4e5f\u8bf4\u4e0d\u8fc7\u53bb\uff1b<\/strong><\/li>\n<li><strong>\u5c3d\u91cf\u4e0d\u4f7f\u7528\u975e<\/strong><strong>ASCII<\/strong><strong>\u5b57\u7b26\uff0c\u5982\u679c\u4f7f\u7528\u7684\u8bdd\uff0c\u53c2\u8003<\/strong><strong>UTF-8<\/strong><strong>\u683c\u5f0f\uff08\u5c24\u5176\u662f<\/strong><strong>UNIX\/Linux<\/strong><strong>\u4e0b\uff0c<\/strong><strong>Windows<\/strong><strong>\u4e0b\u53ef\u4ee5\u8003\u8651\u5bbd\u5b57\u7b26\uff09\uff0c\u5c3d\u91cf\u4e0d\u5c06\u5b57\u7b26\u4e32\u5e38\u91cf\u8026\u5408\u5230\u4ee3\u7801\u4e2d\uff0c\u6bd4\u5982\u72ec\u7acb\u51fa\u8d44\u6e90\u6587\u4ef6\uff0c\u8fd9\u4e0d\u4ec5\u4ec5\u662f\u98ce\u683c\u95ee\u9898\u4e86\uff1b<\/strong><\/li>\n<li><strong> UNIX\/Linux<\/strong><strong>\u4e0b\u65e0\u6761\u4ef6\u4f7f\u7528\u7a7a\u683c\uff0c<\/strong><strong>MSVC<\/strong><strong>\u7684\u8bdd\u4f7f\u7528<\/strong><strong>Tab<\/strong><strong>\u4e5f\u65e0\u53ef\u539a\u975e\uff1b<\/strong><\/li>\n<li><strong>\u51fd\u6570\u53c2\u6570\u3001\u903b\u8f91\u6761\u4ef6\u3001\u521d\u59cb\u5316\u5217\u8868\uff1a\u8981\u4e48\u6240\u6709\u53c2\u6570\u548c\u51fd\u6570\u540d\u653e\u5728\u540c\u4e00\u884c\uff0c\u8981\u4e48\u6240\u6709\u53c2\u6570\u5e76\u6392\u5206\u884c\uff1b<\/strong><\/li>\n<li><strong>\u9664\u51fd\u6570\u5b9a\u4e49\u7684\u5de6\u5927\u62ec\u53f7\u53ef\u4ee5\u7f6e\u4e8e\u884c\u9996\u5916\uff0c\u5305\u62ec\u51fd\u6570<\/strong><strong>\/<\/strong><strong>\u7c7b<\/strong><strong>\/<\/strong><strong>\u7ed3\u6784\u4f53<\/strong><strong>\/<\/strong><strong>\u679a\u4e3e\u58f0\u660e\u3001\u5404\u79cd\u8bed\u53e5\u7684\u5de6\u5927\u62ec\u53f7\u7f6e\u4e8e\u884c\u5c3e\uff0c\u6240\u6709\u53f3\u5927\u62ec\u53f7\u72ec\u7acb\u6210\u884c\uff1b<\/strong><\/li>\n<li><strong> .\/-&gt;<\/strong><strong>\u64cd\u4f5c\u7b26\u524d\u540e\u4e0d\u7559\u7a7a\u683c\uff0c<\/strong><strong>*\/&amp;<\/strong><strong>\u4e0d\u8981\u524d\u540e\u90fd\u7559\uff0c\u4e00\u4e2a\u5c31\u53ef\uff0c\u9760\u5de6\u9760\u53f3\u4f9d\u5404\u4eba\u559c\u597d\uff1b<\/strong><\/li>\n<li><strong>\u9884\u5904\u7406\u6307\u4ee4<\/strong><strong>\/<\/strong><strong>\u547d\u540d\u7a7a\u95f4\u4e0d\u4f7f\u7528\u989d\u5916\u7f29\u8fdb\uff0c\u7c7b<\/strong><strong>\/<\/strong><strong>\u7ed3\u6784\u4f53<\/strong><strong>\/<\/strong><strong>\u679a\u4e3e<\/strong><strong>\/<\/strong><strong>\u51fd\u6570<\/strong><strong>\/<\/strong><strong>\u8bed\u53e5\u4f7f\u7528\u7f29\u8fdb\uff1b<\/strong><\/li>\n<li><strong>\u521d\u59cb\u5316\u7528<\/strong><strong>=<\/strong><strong>\u8fd8\u662f<\/strong><strong>()<\/strong><strong>\u4f9d\u4e2a\u4eba\u559c\u597d\uff0c\u7edf\u4e00\u5c31\u597d\uff1b<\/strong><\/li>\n<li><strong> return<\/strong><strong>\u4e0d\u8981\u52a0<\/strong><strong>()<\/strong><strong>\uff1b<\/strong><\/li>\n<li><strong>\u6c34\u5e73<\/strong><strong>\/<\/strong><strong>\u5782\u76f4\u7559\u767d\u4e0d\u8981\u6ee5\u7528\uff0c\u600e\u4e48\u6613\u8bfb\u600e\u4e48\u6765\u3002<\/strong><\/li>\n<\/ol>\n<h1><a href=\"http:\/\/www.cppblog.com\/Fox\/archive\/2008\/07\/23\/56933.html\">Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u516b\uff09[\u5b8c]<\/a><\/h1>\n<h2>\u89c4\u5219\u4e4b\u4f8b\u5916<\/h2>\n<p>\u524d\u9762\u8bf4\u660e\u7684\u7f16\u7801\u4e60\u60ef\u57fa\u672c\u662f\u5f3a\u5236\u6027\u7684\uff0c\u4f46\u6240\u6709\u4f18\u79c0\u7684\u89c4\u5219\u90fd\u5141\u8bb8\u4f8b\u5916\u3002<\/p>\n<h2>1. \u73b0\u6709\u4e0d\u7edf\u4e00\u4ee3\u7801\uff08Existing Non-conformant Code\uff09<\/h2>\n<p>\u5bf9\u4e8e\u73b0\u6709\u4e0d\u7b26\u5408\u65e2\u5b9a\u7f16\u7a0b\u98ce\u683c\u7684\u4ee3\u7801\u53ef\u4ee5\u7f51\u5f00\u4e00\u9762\u3002<\/p>\n<p>\u5f53\u4f60\u4fee\u6539\u4f7f\u7528\u5176\u4ed6\u98ce\u683c\u7684\u4ee3\u7801\u65f6\uff0c\u4e3a\u4e86\u4e0e\u4ee3\u7801\u539f\u6709\u98ce\u683c\u4fdd\u6301\u4e00\u81f4\u53ef\u4ee5\u4e0d\u4f7f\u7528\u672c\u6307\u5357\u7ea6\u5b9a\u3002\u5982\u679c\u4e0d\u653e\u5fc3\u53ef\u4ee5\u4e0e\u4ee3\u7801\u539f\u4f5c\u8005\u6216\u73b0\u5728\u7684\u8d1f\u8d23\u4eba\u5458\u5546\u8ba8\uff0c\u8bb0\u4f4f\uff0c\u4e00\u81f4\u6027\u5305\u62ec\u539f\u6709\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<h2>1. Windows\u4ee3\u7801\uff08Windows Code\uff09<\/h2>\n<p>Windows\u7a0b\u5e8f\u5458\u6709\u81ea\u5df1\u7684\u7f16\u7801\u4e60\u60ef\uff0c\u4e3b\u8981\u6e90\u4e8eWindows\u7684\u4e00\u4e9b\u5934\u6587\u4ef6\u548c\u5176\u4ed6Microsoft\u4ee3\u7801\u3002\u6211\u4eec\u5e0c\u671b\u4efb\u4f55\u4eba\u90fd\u53ef\u4ee5\u987a\u5229\u8bfb\u61c2\u4f60\u7684\u4ee3\u7801\uff0c\u6240\u4ee5\u9488\u5bf9\u6240\u6709\u5e73\u53f0\u7684C++\u7f16\u7801\u7ed9\u51fa\u4e00\u4e2a\u5355\u72ec\u7684\u6307\u5bfc\u65b9\u6848\u3002<\/p>\n<p>\u5982\u679c\u4f60\u4e00\u76f4\u4f7f\u7528Windows\u7f16\u7801\u98ce\u683c\u7684\uff0c\u8fd9\u513f\u6709\u5fc5\u8981\u91cd\u7533\u4e00\u4e0b\u67d0\u4e9b\u4f60\u53ef\u80fd\u4f1a\u5fd8\u8bb0\u7684\u6307\u5357\uff08\u8bd1\u8005\u6ce8\uff0c\u6211\u600e\u4e48\u611f\u89c9\u50cf\u5728\u88ab\u6d17\u8111:D\uff09\uff1a<\/p>\n<p>1) \u4e0d\u8981\u4f7f\u7528\u5308\u7259\u5229\u547d\u540d\u6cd5\uff08Hungarian notation\uff0c\u5982\u5b9a\u4e49\u6574\u578b\u53d8\u91cf\u4e3aiNum\uff09\uff0c\u4f7f\u7528Google\u547d\u540d\u7ea6\u5b9a\uff0c\u5305\u62ec\u5bf9\u6e90\u6587\u4ef6\u4f7f\u7528.cc\u6269\u5c55\u540d\uff1b<\/p>\n<p>2) Windows\u5b9a\u4e49\u4e86\u5f88\u591a\u539f\u6709\u5185\u5efa\u7c7b\u578b\u7684\u540c\u4e49\u8bcd\uff08\u8bd1\u8005\u6ce8\uff0c\u8fd9\u4e00\u70b9\uff0c\u6211\u4e5f\u5f88\u53cd\u611f\uff09\uff0c\u5982DWORD\u3001HANDLE\u7b49\u7b49\uff0c\u5728\u8c03\u7528Windows API\u65f6\u8fd9\u662f\u5b8c\u5168\u53ef\u4ee5\u63a5\u53d7\u751a\u81f3\u9f13\u52b1\u7684\uff0c\u4f46\u8fd8\u662f\u5c3d\u91cf\u4f7f\u7528\u539f\u6765\u7684C++\u7c7b\u578b\uff0c\u4f8b\u5982\uff0c\u4f7f\u7528const TCHAR *\u800c\u4e0d\u662fLPCTSTR\uff1b<\/p>\n<p>3) \u4f7f\u7528Microsoft Visual C++\u8fdb\u884c\u7f16\u8bd1\u65f6\uff0c\u5c06<strong>\u8b66\u544a\u7ea7\u522b\u8bbe\u7f6e\u4e3a<\/strong><strong>3<\/strong><strong>\u6216\u66f4\u9ad8<\/strong>\uff0c\u5e76<strong>\u5c06\u6240\u6709<\/strong><strong>warnings<\/strong><strong>\u5f53\u4f5c<\/strong><strong>errors<\/strong><strong>\u5904\u7406<\/strong>\uff1b<\/p>\n<p>4) \u4e0d\u8981\u4f7f\u7528#pragma once;\u4f5c\u4e3a<strong>\u5305\u542b\u4fdd\u62a4<\/strong>\uff0c\u4f7f\u7528C++\u6807\u51c6<strong>\u5305\u542b\u4fdd\u62a4<\/strong>\uff0c<strong>\u5305\u542b\u4fdd\u62a4<\/strong>\u7684\u6587\u4ef6\u8def\u5f84\u5305\u542b\u5230\u9879\u76ee\u6811\u9876\u5c42\uff08\u8bd1\u8005\u6ce8\uff0c#include&lt;prj_name\/public\/tools.h&gt;\uff09\uff1b<\/p>\n<p>5) \u9664\u975e\u4e07\u4e0d\u5f97\u5df2\uff0c\u5426\u5219\u4e0d\u4f7f\u7528\u4efb\u4f55\u4e0d\u6807\u51c6\u7684\u6269\u5c55\uff0c\u5982#pragma\u548c__declspec\uff0c\u5141\u8bb8\u4f7f\u7528__declspec(dllimport)\u548c__declspec(dllexport)\uff0c\u4f46\u5fc5\u987b\u901a\u8fc7DLLIMPORT\u548cDLLEXPORT\u7b49\u5b8f\uff0c\u4ee5\u4fbf\u5176\u4ed6\u4eba\u5728\u5171\u4eab\u4f7f\u7528\u8fd9\u4e9b\u4ee3\u7801\u65f6\u5bb9\u6613\u653e\u5f03\u8fd9\u4e9b\u6269\u5c55\u3002<\/p>\n<p>\u5728Windows\u4e0a\uff0c\u53ea\u6709\u5f88\u5c11\u4e00\u4e9b\u5076\u5c14\u53ef\u4ee5\u4e0d\u9075\u5b88\u7684\u89c4\u5219\uff1a<\/p>\n<p>1) \u901a\u5e38\u6211\u4eec<strong>\u7981\u6b62\u4f7f\u7528\u591a\u91cd\u7ee7\u627f<\/strong>\uff0c\u4f46\u5728\u4f7f\u7528<a href=\"http:\/\/en.wikipedia.org\/wiki\/Component_Object_Model\">COM<\/a>\u548c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_Template_Library\">ATL<\/a>\/<a href=\"http:\/\/en.wikipedia.org\/wiki\/WTL\">WTL<\/a>\u7c7b\u65f6\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u7ee7\u627f\uff0c\u4e3a\u4e86\u6267\u884c<a href=\"http:\/\/en.wikipedia.org\/wiki\/Component_Object_Model\">COM<\/a>\u6216<a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_Template_Library\">ATL<\/a>\/<a href=\"http:\/\/en.wikipedia.org\/wiki\/WTL\">WTL<\/a>\u7c7b\u53ca\u5176\u63a5\u53e3\u65f6\u53ef\u4ee5\u4f7f\u7528\u591a\u91cd\u5b9e\u73b0\u7ee7\u627f\uff1b<\/p>\n<p>2) \u867d\u7136<strong>\u4ee3\u7801\u4e2d\u4e0d\u5e94\u4f7f\u7528\u5f02\u5e38<\/strong>\uff0c\u4f46\u5728<a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_Template_Library\">ATL<\/a>\u548c\u90e8\u5206<a href=\"http:\/\/en.wikipedia.org\/wiki\/Standard_Template_Library\">STL<\/a>\uff08\u5305\u62ecVisual C++\u7684STL\uff09\u4e2d\u5f02\u5e38\u88ab\u5e7f\u6cdb\u4f7f\u7528\uff0c\u4f7f\u7528<a href=\"http:\/\/en.wikipedia.org\/wiki\/Active_Template_Library\">ATL<\/a>\u65f6\uff0c\u5e94\u5b9a\u4e49_ATL_NO_EXCEPTIONS\u4ee5\u5c4f\u853d\u5f02\u5e38\uff0c\u4f60\u8981\u7814\u7a76\u4e00\u4e0b\u662f\u5426\u4e5f\u5c4f\u853d\u6389STL\u7684\u5f02\u5e38\uff0c\u5982\u679c\u4e0d\u5c4f\u853d\uff0c\u5f00\u542f\u7f16\u8bd1\u5668\u5f02\u5e38\u4e5f\u53ef\u4ee5\uff0c\u6ce8\u610f\u8fd9\u53ea\u662f\u4e3a\u4e86\u7f16\u8bd1STL\uff0c\u81ea\u5df1\u4ecd\u7136\u4e0d\u8981\u5199\u542b\u5f02\u5e38\u5904\u7406\u7684\u4ee3\u7801\uff1b<\/p>\n<p>3) \u901a\u5e38\u6bcf\u4e2a\u9879\u76ee\u7684\u6bcf\u4e2a\u6e90\u6587\u4ef6\u4e2d\u90fd\u5305\u542b\u4e00\u4e2a\u540d\u4e3aStdAfx.h\u6216precompile.h\u7684\u5934\u6587\u4ef6\u65b9\u4fbf\u5934\u6587\u4ef6\u9884\u7f16\u8bd1\uff0c\u4e3a\u4e86\u4f7f\u4ee3\u7801\u65b9\u4fbf\u4e0e\u5176\u4ed6\u9879\u76ee\u5171\u4eab\uff0c\u907f\u514d\u663e\u5f0f\u5305\u542b\u6b64\u6587\u4ef6\uff08precompile.cc\u9664\u5916\uff09\uff0c\u4f7f\u7528\u7f16\u8bd1\u5668\u9009\u9879\/FI\u4ee5\u81ea\u52a8\u5305\u542b\uff1b<\/p>\n<p>4) \u901a\u5e38\u540d\u4e3aresource.h\u3001\u4e14\u53ea\u5305\u542b\u5b8f\u7684\u8d44\u6e90\u5934\u6587\u4ef6\uff0c\u4e0d\u5fc5\u62d8\u6ce5\u4e8e\u6b64\u98ce\u683c\u6307\u5357\u3002<\/p>\n<h2>\u56e2\u961f\u5408\u4f5c<\/h2>\n<p>\u53c2\u8003\u5e38\u8bc6\uff0c<strong>\u4fdd\u6301\u4e00\u81f4<\/strong>\u3002<\/p>\n<p>\u7f16\u8f91\u4ee3\u7801\u65f6\uff0c\u82b1\u70b9\u65f6\u95f4\u770b\u770b\u9879\u76ee\u4e2d\u7684\u5176\u4ed6\u4ee3\u7801\u5e76\u786e\u5b9a\u5176\u98ce\u683c\uff0c\u5982\u679c\u5176\u4ed6\u4ee3\u7801if\u8bed\u53e5\u4e2d\u4f7f\u7528\u7a7a\u683c\uff0c\u90a3\u4e48\u4f60\u4e5f\u8981\u4f7f\u7528\u3002\u5982\u679c\u5176\u4e2d\u7684\u6ce8\u91ca\u7528\u661f\u53f7\uff08*\uff09\u56f4\u6210\u4e00\u4e2a\u76d2\u5b50\u72b6\uff0c\u4f60\u4e5f\u8fd9\u6837\u505a\uff1a<\/p>\n<p>\/**********************************<\/p>\n<p>* Some comments are here.<\/p>\n<p>* There may be many lines.<\/p>\n<p>**********************************\/<\/p>\n<p>&nbsp;<\/p>\n<p>\u7f16\u7a0b\u98ce\u683c\u6307\u5357\u7684\u4f7f\u7528\u8981\u70b9\u5728\u4e8e\u63d0\u4f9b\u4e00\u4e2a\u516c\u5171\u7684\u7f16\u7801\u89c4\u8303\uff0c\u6240\u6709\u4eba\u53ef\u4ee5\u628a\u7cbe\u529b\u96c6\u4e2d\u5728\u5b9e\u73b0\u5185\u5bb9\u800c\u4e0d\u662f\u8868\u73b0\u5f62\u5f0f\u4e0a\u3002\u6211\u4eec\u7ed9\u51fa\u4e86\u5168\u5c40\u7684\u98ce\u683c\u89c4\u8303\uff0c\u4f46\u5c40\u90e8\u7684\u98ce\u683c\u4e5f\u5f88\u91cd\u8981\uff0c\u5982\u679c\u4f60\u5728\u4e00\u4e2a\u6587\u4ef6\u4e2d\u65b0\u52a0\u7684\u4ee3\u7801\u548c\u539f\u6709\u4ee3\u7801\u98ce\u683c\u76f8\u53bb\u751a\u8fdc\u7684\u8bdd\uff0c\u8fd9\u5c31\u7834\u574f\u4e86\u6587\u4ef6\u672c\u8eab\u7684\u6574\u4f53\u7f8e\u89c2\u4e5f\u5f71\u54cd\u9605\u8bfb\uff0c\u6240\u4ee5\u8981\u5c3d\u91cf\u907f\u514d\u3002<\/p>\n<p>\u597d\u4e86\uff0c\u5173\u4e8e\u7f16\u7801\u98ce\u683c\u5199\u7684\u5dee\u4e0d\u591a\u4e86\uff0c\u4ee3\u7801\u672c\u8eab\u624d\u662f\u66f4\u6709\u8da3\u7684\uff0c\u5c3d\u60c5\u4eab\u53d7\u5427\uff01<\/p>\n<p><em>Benjy Weinberger<\/em><\/p>\n<p><em>Craig Silverstein<\/em><\/p>\n<p><em>Gregory Eitzmann<\/em><\/p>\n<p><em>Mark Mentovai<\/em><\/p>\n<p><em>Tashana Landray<\/em><\/p>\n<p>______________________________________<\/p>\n<p><strong>\u8bd1\u8005\uff1a\u7ec8\u4e8e\u7ffb\u5b8c\u4e86\uff0c\u524d\u540e\u5386\u65f6\u4e24\u5468\uff0c\u6574\u4e2a\u8fc7\u7a0b\u4e2d\uff0c\u867d\u56e0\u5de5\u4f5c\u5173\u7cfb\u5076\u6709\u61c8\u6020\uff0c\u4f46\u603b\u7b97\u4e0d\u662f\u864e\u5934\u86c7\u5c3e\uff08\u8d77\u7801\u6211\u7684\u6001\u5ea6\u662f\u975e\u5e38\u8ba4\u771f\u7684<\/strong><strong>\ud83d\ude00<\/strong><strong>\uff09\uff0c\u65e0\u8bba\u662f\u5426\u80fd\u5bf9\u4f60\u6709\u6240\u88e8\u76ca\uff0c\u5bf9\u6211\u800c\u8a00\uff0c\u81f3\u5c11\u662f\u6e29\u4e60\u4e86\u4e00\u4e9b\u4ee5\u524d\u77e5\u9053\u7684\u77e5\u8bc6\uff0c\u4e5f\u5b66\u5230\u4e86\u4e00\u4e9b\u4e4b\u524d\u4e0d\u77e5\u9053\u7684\u77e5\u8bc6\u3002<\/strong><\/p>\n<p><strong>\u521a\u597d\u8fd9\u4e24\u5929\u8fd8\u4e0d\u662f\u7279\u7d27\u5f20\uff0c\u8d76\u7d27\u7ffb\u5b8c\u4e86\uff0c\u8981\u5f00\u59cb\u5e72\u6d3b\u4e86<\/strong><strong>\u2026\u2026<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Google C++\u7f16\u7a0b\u98ce\u683c\u6307\u5357\uff08\u4e00\uff09 <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"colormag_page_container_layout":"default_layout","colormag_page_sidebar_layout":"default_layout","footnotes":""},"categories":[],"tags":[118,28,67,68],"class_list":["post-628","post","type-post","status-publish","format-standard","hentry","tag-02-","tag-c","tag-google","tag-68"],"_links":{"self":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/628","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/comments?post=628"}],"version-history":[{"count":0,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/posts\/628\/revisions"}],"wp:attachment":[{"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/media?parent=628"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/categories?post=628"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/i007.cc\/wordpress\/wp-json\/wp\/v2\/tags?post=628"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}