{"id":172,"date":"2009-07-12T14:37:00","date_gmt":"2009-07-12T06:37:00","guid":{"rendered":"http:\/\/gsakd878.wordpress.com\/2009\/07\/12\/%e9%ab%98%e8%b4%a8%e9%87%8fc%e6%95%99%e7%a8%8b-%e7%ac%ac5%e7%ab%a0-%e5%b8%b8%e9%87%8f"},"modified":"2009-07-12T14:37:00","modified_gmt":"2009-07-12T06:37:00","slug":"%e9%ab%98%e8%b4%a8%e9%87%8fc%e6%95%99%e7%a8%8b-%e7%ac%ac5%e7%ab%a0-%e5%b8%b8%e9%87%8f","status":"publish","type":"post","link":"https:\/\/www.keyboardmeow.com\/?p=172","title":{"rendered":"\u9ad8\u8d28\u91cfC++\u6559\u7a0b &#8212; \u7b2c5\u7ae0 \u5e38\u91cf"},"content":{"rendered":"<div style=\"margin-top:15px;margin-bottom:15px;\"><span>\u9ad8\u8d28\u91cfC++\u6559\u7a0b -- \u7b2c5\u7ae0 \u5e38\u91cf<\/span><\/div>\n<div style=\"line-height:20px;background-color:#dddddd;height:20px;color:#666666;\">\n\u6765\u6e90:<a href=\"http:\/\/www.vcworld.net\/\" target=\"_blank\" rel=\"noopener\"><font color=\"#0033FF\">www.vcworld.net<\/font><\/a>\u3000<\/div>\n<div style=\"text-align:left;margin-top:20px;width:95%;margin-bottom:25px;\">\n<p><span>\u5e38\u91cf\u662f\u4e00\u79cd\u6807\u8bc6\u7b26\uff0c\u5b83\u7684\u503c\u5728\u8fd0\u884c\u671f\u95f4\u6052\u5b9a\u4e0d\u53d8\u3002C\u8bed\u8a00\u7528<br \/>\n#define\u6765\u5b9a\u4e49\u5e38\u91cf\uff08\u79f0\u4e3a\u5b8f\u5e38\u91cf\uff09\u3002C++ \u8bed\u8a00\u9664\u4e86<br \/>\n#define\u5916\u8fd8\u53ef\u4ee5\u7528const\u6765\u5b9a\u4e49\u5e38\u91cf\uff08\u79f0\u4e3aconst\u5e38\u91cf\uff09\u3002<\/span><\/p>\n<p>\u00a0<\/p>\n<p><strong><font size=\"4\">5.1 \u4e3a\u4ec0\u4e48\u9700\u8981\u5e38\u91cf<\/font><\/strong><\/p>\n<p>\u5982\u679c\u4e0d\u4f7f\u7528\u5e38\u91cf\uff0c\u76f4\u63a5\u5728\u7a0b\u5e8f\u4e2d\u586b\u5199\u6570\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u5c06\u4f1a\u6709\u4ec0\u4e48\u9ebb\u70e6\uff1f<\/p>\n<p>\uff081\uff09 \u7a0b\u5e8f\u7684\u53ef\u8bfb\u6027\uff08\u53ef\u7406\u89e3\u6027\uff09\u53d8\u5dee\u3002\u7a0b\u5e8f\u5458\u81ea\u5df1\u4f1a\u5fd8\u8bb0\u90a3\u4e9b\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u662f\u4ec0\u4e48\u610f\u601d\uff0c\u7528\u6237\u5219\u66f4\u52a0\u4e0d\u77e5\u5b83\u4eec\u4ece\u4f55\u5904\u6765\u3001\u8868\u793a\u4ec0\u4e48\u3002<\/p>\n<p>\uff082\uff09 \u5728\u7a0b\u5e8f\u7684\u5f88\u591a\u5730\u65b9\u8f93\u5165\u540c\u6837\u7684\u6570\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u96be\u4fdd\u4e0d\u53d1\u751f\u4e66\u5199\u9519\u8bef\u3002<\/p>\n<p>\uff083\uff09 \u5982\u679c\u8981\u4fee\u6539\u6570\u5b57\u6216\u5b57\u7b26\u4e32\uff0c\u5219\u4f1a\u5728\u5f88\u591a\u5730\u65b9\u6539\u52a8\uff0c\u65e2\u9ebb\u70e6\u53c8\u5bb9\u6613\u51fa\u9519\u3002<\/p>\n<p>\u25cf\u3010\u89c4\u52195-1-1\u3011 \u5c3d\u91cf\u4f7f\u7528\u542b\u4e49\u76f4\u89c2\u7684\u5e38\u91cf\u6765\u8868\u793a\u90a3\u4e9b\u5c06\u5728\u7a0b\u5e8f\u4e2d\u591a\u6b21\u51fa\u73b0\u7684\u6570\u5b57\u6216\u5b57\u7b26\u4e32\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p>#define MAX 100<\/p>\n<p>const int MAX = 100; \/\/ C++ \u8bed\u8a00\u7684const\u5e38\u91cf<\/p>\n<p>const float PI = 3.14159; \/\/ C++ \u8bed\u8a00\u7684const\u5e38\u91cf<\/p>\n<p>\u00a0<\/p>\n<p><strong><font size=\"4\">5.2 const \u4e0e<br \/>\n#define\u7684\u6bd4\u8f83<\/font><\/strong><\/p>\n<p>C++ \u8bed\u8a00\u53ef\u4ee5\u7528const\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u4e5f\u53ef\u4ee5\u7528 #define\u6765\u5b9a\u4e49\u5e38\u91cf\u3002\u4f46\u662f\u524d\u8005\u6bd4\u540e\u8005\u6709\u66f4\u591a\u7684\u4f18\u70b9\uff1a<\/p>\n<p>\uff081\uff09<br \/>\nconst\u5e38\u91cf\u6709\u6570\u636e\u7c7b\u578b\uff0c\u800c\u5b8f\u5e38\u91cf\u6ca1\u6709\u6570\u636e\u7c7b\u578b\u3002\u7f16\u8bd1\u5668\u53ef\u4ee5\u5bf9\u524d\u8005\u8fdb\u884c\u7c7b\u578b\u5b89\u5168\u68c0\u67e5\u3002\u800c\u5bf9\u540e\u8005\u53ea\u8fdb\u884c\u5b57\u7b26\u66ff\u6362\uff0c\u6ca1\u6709\u7c7b\u578b\u5b89\u5168\u68c0\u67e5\uff0c\u5e76\u4e14\u5728\u5b57\u7b26\u66ff\u6362\u53ef\u80fd\u4f1a\u4ea7\u751f\u610f\u6599\u4e0d\u5230\u7684\u9519\u8bef\uff08\u8fb9\u9645\u6548\u5e94\uff09\u3002<\/p>\n<p>\uff082\uff09 \u6709\u4e9b\u96c6\u6210\u5316\u7684\u8c03\u8bd5\u5de5\u5177\u53ef\u4ee5\u5bf9const\u5e38\u91cf\u8fdb\u884c\u8c03\u8bd5\uff0c\u4f46\u662f\u4e0d\u80fd\u5bf9\u5b8f\u5e38\u91cf\u8fdb\u884c\u8c03\u8bd5\u3002<\/p>\n<p>\u25cf\u3010\u89c4\u52195-2-1\u3011\u5728C++ \u7a0b\u5e8f\u4e2d\u53ea\u4f7f\u7528const\u5e38\u91cf\u800c\u4e0d\u4f7f\u7528\u5b8f\u5e38\u91cf\uff0c\u5373const\u5e38\u91cf\u5b8c\u5168\u53d6\u4ee3\u5b8f\u5e38\u91cf\u3002<\/p>\n<p>\u00a0<\/p>\n<p><strong><font size=\"4\">5.3 \u5e38\u91cf\u5b9a\u4e49\u89c4\u5219<\/font><\/strong><\/p>\n<p>\n\u25cf\u3010\u89c4\u52195-3-1\u3011\u9700\u8981\u5bf9\u5916\u516c\u5f00\u7684\u5e38\u91cf\u653e\u5728\u5934\u6587\u4ef6\u4e2d\uff0c\u4e0d\u9700\u8981\u5bf9\u5916\u516c\u5f00\u7684\u5e38\u91cf\u653e\u5728\u5b9a\u4e49\u6587\u4ef6\u7684\u5934\u90e8\u3002\u4e3a\u4fbf\u4e8e\u7ba1\u7406\uff0c\u53ef\u4ee5\u628a\u4e0d\u540c\u6a21\u5757\u7684\u5e38\u91cf\u96c6\u4e2d\u5b58\u653e\u5728\u4e00\u4e2a\u516c\u5171\u7684\u5934\u6587\u4ef6\u4e2d\u3002<\/p>\n<p>\u25cf\u3010\u89c4\u52195-3-2\u3011\u5982\u679c\u67d0\u4e00\u5e38\u91cf\u4e0e\u5176\u5b83\u5e38\u91cf\u5bc6\u5207\u76f8\u5173\uff0c\u5e94\u5728\u5b9a\u4e49\u4e2d\u5305\u542b\u8fd9\u79cd\u5173\u7cfb\uff0c\u800c\u4e0d\u5e94\u7ed9\u51fa\u4e00\u4e9b\u5b64\u7acb\u7684\u503c\u3002<\/p>\n<p>\u4f8b\u5982\uff1a<\/p>\n<p>const float RADIUS = 100;<\/p>\n<p>const float DIAMETER = RADIUS * 2;<\/p>\n<p>\u00a0<\/p>\n<p><strong><font size=\"4\">5.4 \u7c7b\u4e2d\u7684\u5e38\u91cf<\/font><\/strong><\/p>\n<p>\n\u6709\u65f6\u6211\u4eec\u5e0c\u671b\u67d0\u4e9b\u5e38\u91cf\u53ea\u5728\u7c7b\u4e2d\u6709\u6548\u3002\u7531\u4e8e#define\u5b9a\u4e49\u7684\u5b8f\u5e38\u91cf\u662f\u5168\u5c40\u7684\uff0c\u4e0d\u80fd\u8fbe\u5230\u76ee\u7684\uff0c\u4e8e\u662f\u60f3\u5f53\u7136\u5730\u89c9\u5f97\u5e94\u8be5\u7528const\u4fee\u9970\u6570\u636e\u6210\u5458\u6765\u5b9e\u73b0\u3002const\u6570\u636e\u6210\u5458\u7684\u786e\u662f\u5b58\u5728\u7684\uff0c\u4f46\u5176\u542b\u4e49\u5374\u4e0d\u662f\u6211\u4eec\u6240\u671f\u671b\u7684\u3002const\u6570\u636e\u6210\u5458\u53ea\u5728\u67d0\u4e2a\u5bf9\u8c61\u751f\u5b58\u671f\u5185\u662f\u5e38\u91cf\uff0c\u800c\u5bf9\u4e8e\u6574\u4e2a\u7c7b\u800c\u8a00\u5374\u662f\u53ef\u53d8\u7684\uff0c\u56e0\u4e3a\u7c7b\u53ef\u4ee5\u521b\u5efa\u591a\u4e2a\u5bf9\u8c61\uff0c\u4e0d\u540c\u7684\u5bf9\u8c61\u5176const\u6570\u636e\u6210\u5458\u7684\u503c\u53ef\u4ee5\u4e0d\u540c\u3002<\/p>\n<p>\u4e0d\u80fd\u5728\u7c7b\u58f0\u660e\u4e2d\u521d\u59cb\u5316const\u6570\u636e\u6210\u5458\u3002\u4ee5\u4e0b\u7528\u6cd5\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u7c7b\u7684\u5bf9\u8c61\u672a\u88ab\u521b\u5efa\u65f6\uff0c\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053SIZE\u7684\u503c\u662f\u4ec0\u4e48\u3002<\/p>\n<p>class A<\/p>\n<p>{\u2026<\/p>\n<p>const int SIZE = 100; \/\/ \u9519\u8bef\uff0c\u4f01\u56fe\u5728\u7c7b\u58f0\u660e\u4e2d\u521d\u59cb\u5316const\u6570\u636e\u6210\u5458<\/p>\n<p>int array[SIZE]; \/\/ \u9519\u8bef\uff0c\u672a\u77e5\u7684SIZE<\/p>\n<p>};<\/p>\n<p>const\u6570\u636e\u6210\u5458\u7684\u521d\u59cb\u5316\u53ea\u80fd\u5728\u7c7b\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u8868\u4e2d\u8fdb\u884c\uff0c\u4f8b\u5982<\/p>\n<p>class A<\/p>\n<p>{\u2026<\/p>\n<p>A(int size); \/\/ \u6784\u9020\u51fd\u6570<\/p>\n<p>const int SIZE ;<\/p>\n<p>};<\/p>\n<p>A::A(int size) : SIZE(size) \/\/ \u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u8868<\/p>\n<p>{<\/p>\n<p>\u2026<\/p>\n<p>}<\/p>\n<p>A a(100); \/\/ \u5bf9\u8c61 a \u7684SIZE\u503c\u4e3a100<\/p>\n<p>A b(200); \/\/ \u5bf9\u8c61 b \u7684SIZE\u503c\u4e3a200<\/p>\n<p>\u600e\u6837\u624d\u80fd\u5efa\u7acb\u5728\u6574\u4e2a\u7c7b\u4e2d\u90fd\u6052\u5b9a\u7684\u5e38\u91cf\u5462\uff1f\u522b\u6307\u671bconst\u6570\u636e\u6210\u5458\u4e86\uff0c\u5e94\u8be5\u7528\u7c7b\u4e2d\u7684\u679a\u4e3e\u5e38\u91cf\u6765\u5b9e\u73b0\u3002\u4f8b\u5982<\/p>\n<p>class A<\/p>\n<p>{\u2026<\/p>\n<p>enum { SIZE1 = 100, SIZE2 = 200}; \/\/ \u679a\u4e3e\u5e38\u91cf<\/p>\n<p>int array1[SIZE1];<\/p>\n<p>int array2[SIZE2];<\/p>\n<p>};<\/p>\n<p>\n\u679a\u4e3e\u5e38\u91cf\u4e0d\u4f1a\u5360\u7528\u5bf9\u8c61\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u5b83\u4eec\u5728\u7f16\u8bd1\u65f6\u88ab\u5168\u90e8\u6c42\u503c\u3002\u679a\u4e3e\u5e38\u91cf\u7684\u7f3a\u70b9\u662f\uff1a\u5b83\u7684\u9690\u542b\u6570\u636e\u7c7b\u578b\u662f\u6574\u6570\uff0c\u5176\u6700\u5927\u503c\u6709\u9650\uff0c\u4e14\u4e0d\u80fd\u8868\u793a\u6d6e\u70b9\u6570\uff08\u5982PI=3.14159\uff09\u3002<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9ad8\u8d28\u91cfC++\u6559\u7a0b &#8212; \u7b2c5\u7ae0 \u5e38\u91cf \u6765\u6e90:www.vcworld.net\u3000 \u5e38\u91cf\u662f\u4e00\u79cd\u6807\u8bc6\u7b26\uff0c\u5b83\u7684\u503c\u5728\u8fd0\u884c\u671f &hellip; <a href=\"https:\/\/www.keyboardmeow.com\/?p=172\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">\u201c\u9ad8\u8d28\u91cfC++\u6559\u7a0b &#8212; \u7b2c5\u7ae0 \u5e38\u91cf\u201d<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[24,83,123,122,129,146],"class_list":["post-172","post","type-post","status-publish","format-standard","hentry","category-c","tag-c","tag-it","tag-123","tag-122","tag-129","tag-146"],"_links":{"self":[{"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=\/wp\/v2\/posts\/172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=172"}],"version-history":[{"count":0,"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=\/wp\/v2\/posts\/172\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.keyboardmeow.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}