{"id":616,"date":"2023-10-27T21:31:52","date_gmt":"2023-10-27T13:31:52","guid":{"rendered":"https:\/\/wp.minicoda.com\/?p=616"},"modified":"2025-03-01T07:32:00","modified_gmt":"2025-02-28T23:32:00","slug":"const","status":"publish","type":"post","link":"https:\/\/wp.minicoda.com\/index.php\/const\/","title":{"rendered":"const\u5e38\u91cf\u7684\u4e0d\u540c\u5f62\u5f0f"},"content":{"rendered":"\n<p>  const\u5173\u952e\u5b57\u51fa\u73b0\u5728\u4e0d\u5c11\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5b83\u662f\u9632\u5fa1\u6027\u63cf\u8ff0\uff0c\u63d0\u9192\u7f16\u8bd1\u5668\u6ce8\u610f\u76f8\u5e94\u7684\u53d8\u91cf\u6216\u5e38\u91cf\u4e0d\u80fd\u88ab\u4fee\u6539\u3002\u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u68c0\u6d4bhack\u523b\u610f\u8eb2\u8fc7\u68c0\u67e5\u7684\u884c\u4e3a\u3002<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e0d\u540c\u8bed\u8a00\u7684const<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\/ObjC\u8bed\u8a00\u7684const\u4fee\u9970\u7684\u4e0d\u662f\u5e38\u89c4\u7684\u201d\u5e38\u91cf\u201d\uff0c\u800c\u662f\u201d\u4e0d\u53ef\u4fee\u6539\u201d\u7684\u53d8\u91cf\u3002\u4f46\u5b9e\u9645\u4e0a\uff0c\u548c\u5e38\u91cf\u6548\u679c\u4e00\u6837\u3002\u672c\u8d28\u5728\u4e8eC\u8bed\u8a00\u6839\u672c\u6ca1\u6709\u6240\u8c13\u5e38\u91cf\u7684\u6982\u5ff5\uff0c\u7528const\u6765\u5047\u88c5\u6709\u5e38\u91cf\u3002<\/li>\n\n\n\n<li>C++ const\u771f\u7684\u662f\u5e38\u91cf\uff0c\u6bd4C\u8bed\u8a00\u7684\u4f7f\u7528\u66f4\u5e7f\u6cdb\uff0c\u5c24\u5176\u662f\u7c7b\u6210\u5458\u51fd\u6570\u7528\u5230const\u5f15\u7528\uff0ciostream\u5c31\u5305\u542b\u5f88\u591aconst\u4fee\u9970\u7b26\u3002C++17\u5f15\u5165\u4e86constexpr\u5173\u952e\u5b57\uff0c\u53ef\u4fee\u9970\u65b0\u7684\u7c7b\u578b\uff0c\u5982\u51fd\u6570\u3001\u7c7b\u7b49\uff0c\u4f7f\u5f97\u5e38\u91cf\u63a7\u5236\u66f4\u5f3a\u5927\u3002\n<ul class=\"wp-block-list\">\n<li>constexpr int get_size() { return 3; }<\/li>\n\n\n\n<li>char arr[get_size()]; \/\/ \u5408\u6cd5!<\/li>\n\n\n\n<li>\u5c3d\u7ba1\u5982\u6b64\uff0cconstexpr\u4e0d\u4e00\u5b9a\u4fdd\u8bc1\u7f16\u8bd1\u671f\u5b8c\u6210\uff0cC++20\u53c8\u5f15\u5165\u4e86consteval\u786e\u4fdd\u4e00\u5b9a\u662f\u7f16\u8bd1\u671f\u5b8c\u6210\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Java\u7528final\u4ee3\u8868\u5e38\u91cf\uff0c\u6709\u610f\u601d\u7684\u662fconst\u5c5e\u4e8eJava\u7684\u9884\u7559\u5173\u952e\u5b57\uff0c\u4f46\u4e0d\u80fd\u7528\u3002<\/li>\n\n\n\n<li>C#\u9664\u4e86\u6709const\uff0c\u53d7\u5c40\u9650\u4e8e\u58f0\u660e\u65f6\u5c31\u5fc5\u987b\u521d\u59cb\u5316\u3002readonly\u53d8\u91cf\u53ef\u4ee5\u63a8\u8fdf\u5230\u6784\u9020\u51fd\u6570\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li>Python\u6ca1\u6709const\u5173\u952e\u5b57\uff0c\u6309\u7167\u7ea6\u5b9a\u5e38\u91cf\u7528\u5927\u5199\u5b57\u6bcd\uff0c\u5b9e\u9645\u4e0a\u8bed\u6cd5\u4e0a\u6ca1\u6709\u9650\u5b9a\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u53ef\u80fd\u6e90\u4e8ePython\u8bbe\u8ba1\u5bf9\u5e38\u91cf\u7684\u671f\u5f85\u5f88\u4f4e\u3002<\/li>\n\n\n\n<li>Go\u652f\u6301\u5355\u4e2a\u6216\u591a\u4e2a\u6807\u8bc6\u7b26\u7684const\u521d\u59cb\u5316\uff0c\u591a\u4e2a\u6807\u8bc6\u7b26\u521d\u59cb\u5316\u9700\u8981\u7528\u62ec\u53f7\u5305\u8d77\u6765\u3002<\/li>\n\n\n\n<li>Rust\u652f\u6301const\u5e38\u91cf\uff0c\u5c5e\u4e8e\u7f16\u8bd1\u671f\u5e38\u91cf\uff0clet\u867d\u7136\u4e5f\u4ee3\u8868\u5e38\u91cf\uff0c\u5c5e\u4e8e\u8fd0\u884c\u671f\u5e38\u91cf\uff0c\u52a0mut\u4ee3\u8868\u8fd0\u884c\u671f\u53d8\u91cf.<\/li>\n\n\n\n<li>JS\u5728ES6\u4e4b\u540e\u5f15\u5165\u4e86const\u5173\u952e\u5b57\u4ee3\u8868\u5e38\u91cf\uff0clet\u58f0\u660e\u7684\u53d8\u91cf\u5c5e\u4e8e\u53d8\u91cf\u3002var\u5c5e\u4e8edeprecated\u7684\u51fd\u6570\u5185\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>Swift\/\u4ed3\u9889 \u6ca1\u6709const\u5173\u952e\u5b57, \u7528let\u4ee3\u8868\u5e38\u91cf\uff0cvar\u4ee3\u8868\u53d8\u91cf\u3002<\/li>\n\n\n\n<li>VB\/Pascal \u7528Const\u4ee3\u8868\u5e38\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">const\u7684\u6307\u9488\u8fd8\u662f\u5e38\u91cf?<\/h2>\n\n\n\n<p>  \u6709\u6307\u9488\u7684\u7f16\u7a0b\u8bed\u8a00\u5c31\u662f\u590d\u6742\uff0cconst\u548c\u6307\u9488\u7684\u7ec4\u5408\u8ba9\u5927\u5bb6\u6478\u4e0d\u5230\u5934\u8111\u3002\u7a76\u7adf\u662f\u53d8\u91cf\u662fconst\u8fd8\u662f\u6307\u9488const\uff1f\u4e00\u4e2a\u6bd4\u8f83\u6709\u610f\u601d\u7684\u539f\u5219\u662f\uff0c\u201d\u4ece\u53f3\u5411\u5de6\u8bfb\u201d\u7684\u89e3\u6790\u65b9\u6cd5\u5f88\u5b9e\u7528\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>C\u8bed\u8a00\uff1a\u201dconst int * const p\u201d\u89e3\u8bfb\u65b9\u5f0f\u662f\u201d\u4ece\u53f3\u5411\u5de6\u201d\u8bfb\u7684\u65b9\u6cd5\n<ul class=\"wp-block-list\">\n<li>const int * p\u662f\u201dp\u201d \u201c\u6307\u5411\u201d \u201c\u6574\u5f62\u201d \u201c\u5e38\u91cf\u201d.<\/li>\n\n\n\n<li>const int * const p\u662f\u201dp\u201d \u201c\u5e38\u201d \u201c\u6307\u5411\u201d \u201c\u6574\u5f62\u201d \u201c\u5e38\u91cf\u201d.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">const\u548c\u4ee3\u7801\u5927\u5c0f<\/h2>\n\n\n\n<p>\u4e00\u822c\u800c\u8a00\u76f8\u6bd4\u6ca1\u6709const\u4fee\u9970\uff0c\u52a0const\u4fee\u9970\u540e\u4e0d\u4f1a\u589e\u52a0\u4ee3\u7801\u5927\u5c0f\uff0c\u53ef\u80fd\u51cf\u5c0f\u4ee3\u7801\u5927\u5c0f\u3002\u7f16\u8bd1\u5668\u5229\u7528const\u4fee\u9970\u4f18\u5316\u5bf9\u5e94\u7684\u53d8\u91cf\uff0c\u5982\u679c\u53ef\u80fd\u7528\u5e38\u6570\u4ee3\u66ff\u6216\u8005\u5f15\u7528\u5df2\u6709\u53d8\u91cf\u800c\u4e0d\u62f7\u8d1d\u65b0\u7684\u53d8\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>const\u5173\u952e\u5b57\u51fa\u73b0\u5728\u4e0d\u5c11\u7f16\u7a0b\u8bed\u8a00\u4e2d\uff0c\u5b83\u662f\u9632\u5fa1\u6027\u63cf\u8ff0\uff0c\u63d0\u9192\u7f16\u8bd1\u5668\u6ce8\u610f\u76f8\u5e94\u7684\u53d8\u91cf\u6216\u5e38\u91cf\u4e0d\u80fd\u88ab\u4fee\u6539\u3002\u5f53\u7136\uff0c\u7f16\u8bd1\u5668\u65e0\u6cd5\u68c0\u6d4bhack\u523b\u610f\u8eb2\u8fc7\u68c0\u67e5\u7684\u884c\u4e3a\u3002 \u4e0d\u540c\u8bed\u8a00\u7684const const\u7684\u6307\u9488\u8fd8\u662f\u5e38\u91cf? \u6709\u6307&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[234,9],"class_list":["post-616","post","type-post","status-publish","format-standard","hentry","category-8","tag-const","tag-9"],"views":95,"_links":{"self":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/616","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/comments?post=616"}],"version-history":[{"count":6,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions"}],"predecessor-version":[{"id":3769,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/posts\/616\/revisions\/3769"}],"wp:attachment":[{"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/media?parent=616"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/categories?post=616"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/wp.minicoda.com\/index.php\/wp-json\/wp\/v2\/tags?post=616"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}