{"id":407,"date":"2010-04-19T16:11:11","date_gmt":"2010-04-19T08:11:11","guid":{"rendered":"http:\/\/115.29.108.60\/?p=407"},"modified":"2010-10-02T19:48:32","modified_gmt":"2010-10-02T11:48:32","slug":"c-primer-chapter4-2","status":"publish","type":"post","link":"http:\/\/115.29.108.60\/?p=407","title":{"rendered":"C++ Primer Chapter5"},"content":{"rendered":"<p>1\u3001\u8868\u8fbe\u5f0f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u64cd\u4f5c\u6570\u901a\u8fc7\u64cd\u4f5c\u7b26\u7ec4\u5408\u800c\u6210\u3002\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7ed3\u679c\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u6ca1\u6709\u64cd\u4f5c\u7b26\uff0c\u5219\u5176\u7ed3\u679c\u5c31\u662f\u64cd\u4f5c\u6570\u672c\u8eab\u7684\u503c\u3002\u5bf9\u4e8e\u542b\u6709\u64cd\u4f5c\u7b26\u7684\u8868\u8fbe\u5f0f\uff0c\u5b83\u7684\u503c\u901a\u8fc7\u5bf9\u64cd\u4f5c\u6570\u505a\u6307\u5b9a\u64cd\u4f5c\u83b7\u5f97\u3002\u9664\u4e86\u7279\u6b8a\u7528\u6cd5\u5916\uff0c\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u662f\u53f3\u503c\uff0c\u53ef\u4ee5\u8bfb\u53d6\u8be5\u7ed3\u679c\u503c\uff0c\u4f46\u662f\u4e0d\u5141\u8bb8\u5bf9\u5b83\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n<p>2\u3001\u5982\u679c\u4e24\u4e2a\u64cd\u4f5c\u6570\u90fd\u662f\u8d1f\u6570\uff0c\u6c42\u6a21\u64cd\u4f5c\u7684\u7ed3\u679c\u5219\u4e3a\u8d1f\u6570\uff08\u6216\u96f6\uff09\uff1b\u5982\u679c\u53ea\u6709\u4e00\u4e2a\u64cd\u4f5c\u6570\u4e3a\u8d1f\u6570\uff0c\u9664\u6cd5\u64cd\u4f5c\u548c\u6c42\u6a21\u64cd\u4f5c\u7684\u7ed3\u679c\u53d6\u51b3\u4e8e\u673a\u5668\u3002<\/p>\n<p>3\u3001\u5bf9\u4e8e\u4f4d\u64cd\u4f5c\u7b26\uff0c\u7531\u4e8e\u7cfb\u7edf\u4e0d\u80fd\u786e\u4fdd\u5982\u4f55\u5904\u7406\u5176\u64cd\u4f5c\u6570\u7684\u7b26\u53f7\u4f4d\uff0c\u6240\u4ee5\u5f3a\u70c8\u5efa\u8bae\u4f7f\u7528unsigned\u6574\u578b\u64cd\u4f5c\u6570\u3002<\/p>\n<p>4\u3001\u8d4b\u503c\u64cd\u4f5c\u7b26\u7684\u5de6\u64cd\u4f5c\u6570\u5fc5\u987b\u662f\u975econst\u7684\u5de6\u503c\u3002\u6570\u7ec4\u540d\u662f\u4e0d\u53ef\u4fee\u6539\u7684\u5de6\u503c\uff1a\u56e0\u6b64\u6570\u7ec4\u4e0d\u53ef\u7528\u4f5c\u8d4b\u503c\u64cd\u4f5c\u7684\u76ee\u6807\u3002<\/p>\n<p>\u4e0e\u4e0b\u6807\u548c\u89e3\u5f15\u7528\u64cd\u4f5c\u7b26\u4e00\u6837\uff0c\u8d4b\u503c\u64cd\u4f5c\u7b26\u4e5f\u8fd4\u56de\u5de6\u503c\u3002<\/p>\n<p>5\u3001\u70b9\u64cd\u4f5c\u7b26\u7528\u6237\u83b7\u53d6\u7c7b\u7c7b\u578b\u5bf9\u8c61\u7684\u6210\u5458\u3002<\/p>\n<p>6\u3001sizeof\u64cd\u4f5c\u7b26\u7684\u4f5c\u7528\u662f\u8fd4\u56de\u4e00\u4e2a\u5bf9\u8c61\u6216\u7c7b\u59d3\u540d\u7684\u957f\u5ea6\uff0c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u4e3asize_t\uff0c\u957f\u5ea6\u7684\u5355\u4f4d\u662f\u5b57\u8282\u3002\u53c2\u8003\u8d44\u6599\uff1a<a href=\"http:\/\/115.29.108.60\/?p=498\" target=\"_blank\">sizeof<\/a><\/p>\n<p>7\u3001\u9017\u53f7\u8868\u8fbe\u5f0f\u7684\u7ed3\u679c\u662f\u5176\u6700\u53f3\u8fb9\u8868\u8fbe\u5f0f\u7684\u503c\u3002<\/p>\n<p>8\u3001\u53ea\u6709\u5f53\u64cd\u4f5c\u7b26\u7684\u4e24\u4e2a\u64cd\u4f5c\u6570\u6d89\u53ca\u5230\u540c\u4e00\u4e2a\u5bf9\u8c61\uff0c\u5e76\u6539\u53d8\u5176\u503c\u65f6\uff0c\u64cd\u4f5c\u6570\u7684\u8ba1\u7b97\u6b21\u5e8f\u624d\u4f1a\u5f71\u54cd\u7ed3\u679c\u3002<\/p>\n<p>9\u3001\u5982\u679c\u4e0d\u63d0\u4f9b\u663e\u793a\u521d\u59cb\u5316\uff0c\u52a8\u6001\u521b\u5efa\u7684\u5bf9\u8c61\u4e0e\u5728\u51fd\u6570\u5185\u5b9a\u4e49\u7684\u53d8\u91cf\u521d\u59cb\u5316\u65b9\u5f0f\u76f8\u540c\u3002\u5bf9\u4e8e\u7c7b\u7c7b\u578b\u7684\u5bf9\u8c61\uff0c\u7528\u8be5\u7c7b\u7684\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\uff1b\u800c\u5185\u7f6e\u7c7b\u578b\u7684\u5bf9\u8c61\u5219\u65e0\u521d\u59cb\u5316\u3002<\/p>\n<p>\u4e00\u65e6\u5220\u9664\u4e86\u6307\u9488\u6240\u6307\u5411\u7684\u5bf9\u8c61\uff0c\u7acb\u5373\u5c06\u6307\u9488\u7f6e\u4e3a0\uff0c\u8fd9\u6837\u5c31\u975e\u5e38\u6e05\u695a\u5730\u8868\u660e\u6307\u9488\u4e0d\u518d\u6307\u5411\u4efb\u4f55\u5bf9\u8c61\u3002<\/p>\n<p>\u5982\u679c\u5176\u4ed6\u52a8\u6001\u5bf9\u8c61\u4e00\u6837\uff0cconst\u52a8\u6001\u5bf9\u8c61\u4e5f\u662f\u4f7f\u7528\u5220\u9664\u6307\u9488\u6765\u91ca\u653e\u7684\u3002<\/p>\n<p>10\u3001\u5bf9\u4e8e\u5305\u542bsigned\u548cunsigned int\u578b\u7684\u8868\u8fbe\u5f0f\uff0c\u5176\u8f6c\u6362\u53ef\u80fd\u51fa\u4e4e\u6211\u4eec\u7684\u610f\u6599\u3002\u8868\u8fbe\u5f0f\u4e2d\u7684signed\u578b\u6570\u503c\u4f1a\u88ab\u8f6c\u6362\u4e3aunsigned\u578b\u3002<\/p>\n<p>11\u3001\u4e0d\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6307\u9488\u7684\u4f8b\u5916\u60c5\u51b5\u6709\uff1a\u6570\u7ec4\u7528\u4f5c\u53d6\u5730\u5740\uff08&amp;\uff09\u64cd\u4f5c\u7b26\u7684\u64cd\u4f5c\u6570\u6216sizeof\u64cd\u4f5c\u7b26\u7684\u64cd\u4f5c\u6570\u65f6\uff0c\u6216\u7528\u6570\u7ec4\u5bf9\u6570\u7ec4\u7684\u5f15\u7528\u8fdb\u884c\u521d\u59cb\u5316\u65f6\uff0c\u4e0d\u4f1a\u5c06\u6570\u7ec4\u8f6c\u6362\u4e3a\u6307\u9488\u3002\u53c2\u8003\u8d44\u6599\uff1a<a href=\"http:\/\/115.29.108.60\/?p=404\" target=\"_blank\">C++ Primer Chapter4<\/a><\/p>\n<p>12\u3001\u547d\u540d\u7684\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u7b26\u53f7\u7684\u4e00\u822c\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n<p>cast-name&lt;type&gt;(expression)<\/p>\n<p>\u5176\u4e2dcast-name\u4e3astatic_cast\u3001dynamic_cast\u3001const_cast\u548creinterpret_cast\u4e4b\u4e00\uff0ctype\u4e3a\u8f6c\u6362\u7684\u76ee\u6807\u7c7b\u578b\uff0c\u800cexpression\u5219\u662f\u88ab\u5f3a\u5236\u8f6c\u6362\u7684\u503c\u3002\u5f3a\u5236\u8f6c\u6362\u7684\u7c7b\u578b\u6307\u5b9a\u4e86\u5728expression\u4e0a\u6267\u884c\u67d0\u79cd\u7279\u5b9a\u7c7b\u578b\u7684\u8f6c\u6362\u3002<\/p>\n<p>dynamic_cast\u652f\u6301\u8fd0\u884c\u65f6\u8bc6\u522b\u6307\u9488\u6216\u5f15\u7528\u6240\u6307\u5411\u7684\u5bf9\u8c61\u3002<\/p>\n<p>const_cast\u5c06\u8f6c\u6362\u6389\u8868\u8fbe\u5f0f\u7684const\u6027\u8d28\u3002\u53ea\u6709\u4f7f\u7528const_cast\u624d\u80fd\u5c06const\u6027\u8d28\u8f6c\u6362\u6389\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u8bd5\u56fe\u4f7f\u7528\u5176\u4ed6\u4e09\u79cd\u5f62\u5f0f\u7684\u8f6c\u6362\u90fd\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u65f6\u7684\u9519\u8bef\u3002\u7c7b\u4f3c\u5730\uff0c\u9664\u4e86\u6dfb\u52a0\u6216\u5220\u9664const\u7279\u6027\uff0c\u7528const_cast\u7b26\u6765\u6267\u884c\u5176\u4ed6\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362\uff0c\u90fd\u4f1a\u5f15\u8d77\u7f16\u8bd1\u9519\u8bef\u3002<\/p>\n<p>static_cast\u7f16\u8bd1\u5668\u9690\u5f0f\u6267\u884c\u7684\u4efb\u4f55\u7c7b\u578b\u8f6c\u6362\u90fd\u53ef\u4ee5\u7531\u5b83\u663e\u793a\u5b8c\u6210\u3002\uff08\u5c1d\u8bd5\u8f6c\u6362\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\u5230\u53e6\u4e00\u4e2a\u65e0\u5173\u7684\u7c7b\u578b\u5c06\u5931\u8d25\u3002\uff09<\/p>\n<p>reinterpret_cast\u901a\u5e38\u4e3a\u64cd\u4f5c\u6570\u7684\u4f4d\u6a21\u5f0f\u63d0\u4f9b\u8f83\u4f4e\u5c42\u6b21\u7684\u91cd\u65b0\u89e3\u91ca\u3002\uff08\u603b\u662f\u6210\u529f\u7684\u201c\u6b3a\u9a97\u201d\u7f16\u8bd1\u5668\uff1a\u8f6c\u6362\u4e00\u4e2a\u5bf9\u8c61\u7684\u7c7b\u578b\u5230\u53e6\u4e00\u4e2a\u65e0\u5173\u7684\u7c7b\u578b\u3002\uff09<\/p>\n<p>\u53c2\u8003\u8d44\u6599\uff1a<a href=\"http:\/\/baike.baidu.com\/view\/1263731.htm\" target=\"_blank\">reinterpret_cast<\/a><\/p>\n<p><a href=\"http:\/\/tech.ccidnet.com\/art\/12013\/20090707\/1820823_1.html\" target=\"_blank\">static_cast\u548creinterpret_cast\u7684\u533a\u522b<\/a><\/p>\n<p><a href=\"http:\/\/blog.csdn.net\/dengjiang1999\/archive\/2008\/11\/28\/3403939.aspx\" target=\"_blank\">reinterpret_cast\u7684\u672c\u8d28(\u8f6c)<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1\u3001\u8868\u8fbe\u5f0f\u7531\u4e00\u4e2a\u6216\u591a\u4e2a\u64cd\u4f5c\u6570\u901a\u8fc7\u64cd\u4f5c\u7b26\u7ec4\u5408\u800c\u6210\u3002\u6bcf\u4e2a\u8868\u8fbe\u5f0f\u90fd\u4f1a\u4ea7\u751f\u4e00\u4e2a\u7ed3\u679c\u3002\u5982\u679c\u8868\u8fbe\u5f0f\u4e2d\u6ca1\u6709\u64cd\u4f5c\u7b26\uff0c\u5219\u5176\u7ed3\u679c\u5c31 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-407","post","type-post","status-publish","format-standard","hentry","category-cplusplusprimer"],"_links":{"self":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=407"}],"version-history":[{"count":5,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions"}],"predecessor-version":[{"id":409,"href":"http:\/\/115.29.108.60\/index.php?rest_route=\/wp\/v2\/posts\/407\/revisions\/409"}],"wp:attachment":[{"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=407"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/115.29.108.60\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}