{"id":3241,"date":"2025-03-27T23:54:50","date_gmt":"2025-03-27T15:54:50","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3241"},"modified":"2025-03-27T23:54:50","modified_gmt":"2025-03-27T15:54:50","slug":"c-%e5%b8%b8%e9%87%8f%ef%bc%88constants-in-c%ef%bc%89","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/03\/27\/c-%e5%b8%b8%e9%87%8f%ef%bc%88constants-in-c%ef%bc%89\/","title":{"rendered":"C++ \u5e38\u91cf\uff08Constants in C++\uff09"},"content":{"rendered":"\n<p>\u5e38\u91cf\u662f\u6307<strong>\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u5176\u503c\u4e0d\u4f1a\u6539\u53d8<\/strong>\u7684\u53d8\u91cf\u3002\u5e38\u91cf\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u5e76\u4e14\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u8fd9\u4e9b\u503c\u3002C++ \u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u5982 <strong><code>const<\/code><\/strong> \u5173\u952e\u5b57\u3001<strong><code>constexpr<\/code><\/strong> \u5173\u952e\u5b57\uff0c\u4ee5\u53ca <strong><code>#define<\/code><\/strong> \u5b8f\u5b9a\u4e49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528 <code>const<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u5e38\u91cf<\/strong><\/h2>\n\n\n\n<p><code>const<\/code> \u5173\u952e\u5b57\u7528\u4e8e\u58f0\u660e\u5e38\u91cf\uff0c\u5b83\u53ef\u4ee5\u7528\u4e8e <strong>\u57fa\u7840\u6570\u636e\u7c7b\u578b\u3001\u6570\u7ec4\u3001\u6307\u9488\u3001\u5f15\u7528<\/strong> \u7b49\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1.1 \u57fa\u672c\u5e38\u91cf<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    const int MAX_VALUE = 100;  \/\/ \u58f0\u660e\u4e00\u4e2a\u5e38\u91cf\n    cout &lt;&lt; \"MAX_VALUE = \" &lt;&lt; MAX_VALUE &lt;&lt; endl;\n\n    \/\/ MAX_VALUE = 200;  \/\/ \u9519\u8bef\uff1a\u5e38\u91cf\u503c\u4e0d\u53ef\u4fee\u6539\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MAX_VALUE = 100<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u5e38\u91cf\u5728\u58f0\u660e\u65f6\u5fc5\u987b\u521d\u59cb\u5316<\/strong>\uff0c\u5e76\u4e14\u5728\u7a0b\u5e8f\u4e2d<strong>\u4e0d\u80fd\u4fee\u6539<\/strong>\u5176\u503c\u3002<\/li>\n\n\n\n<li><code>const<\/code> \u5e38\u91cf\u7684\u4f5c\u7528\u8303\u56f4\u53d6\u51b3\u4e8e\u58f0\u660e\u7684\u4f4d\u7f6e\uff0c\u53ef\u4ee5\u662f <strong>\u5c40\u90e8\u5e38\u91cf<\/strong> \u6216 <strong>\u5168\u5c40\u5e38\u91cf<\/strong>\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528 <code>constexpr<\/code> \u5173\u952e\u5b57\u5b9a\u4e49\u5e38\u91cf<\/strong><\/h2>\n\n\n\n<p><code>constexpr<\/code> \u7528\u4e8e\u58f0\u660e\u5e38\u91cf\u8868\u8fbe\u5f0f\uff0c\u8868\u793a\u8be5\u503c\u5728<strong>\u7f16\u8bd1\u65f6\u5df2\u77e5<\/strong>\uff0c\u5e76\u4e14\u5fc5\u987b\u662f <strong>\u5e38\u91cf\u8868\u8fbe\u5f0f<\/strong>\u3002\u5b83\u5e38\u7528\u4e8e\u9700\u8981\u5728\u7f16\u8bd1\u65f6\u8ba1\u7b97\u7684\u5e38\u91cf\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2.1 <code>constexpr<\/code> \u5e38\u91cf\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nconstexpr int square(int x) {\n    return x * x;  \/\/ \u7f16\u8bd1\u65f6\u8ba1\u7b97\n}\n\nint main() {\n    constexpr int size = 10;\n    int array&#91;size];  \/\/ \u4f7f\u7528 constexpr \u5e38\u91cf\u5b9a\u4e49\u6570\u7ec4\u5927\u5c0f\n\n    cout &lt;&lt; \"Square of 5 = \" &lt;&lt; square(5) &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Square of 5 = 25<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>constexpr<\/code> \u5e38\u91cf\u5fc5\u987b\u662f\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u503c\u3002<\/li>\n\n\n\n<li><strong>\u5e38\u91cf\u8868\u8fbe\u5f0f<\/strong> \u5141\u8bb8\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u65f6\u8ba1\u7b97\u7ed3\u679c\uff0c\u8fdb\u800c\u4f18\u5316\u7a0b\u5e8f\u6027\u80fd\u3002<\/li>\n\n\n\n<li><code>constexpr<\/code> \u53ef\u4ee5\u7528\u4e8e\u5e38\u91cf\u51fd\u6570\u3001\u5e38\u91cf\u6570\u7ec4\u5927\u5c0f\u7b49\u573a\u666f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. \u5e38\u91cf\u6307\u9488\u4e0e\u6307\u9488\u5e38\u91cf<\/strong><\/h2>\n\n\n\n<p>\u5e38\u91cf\u4e0d\u4ec5\u9650\u4e8e\u666e\u901a\u6570\u636e\u7c7b\u578b\uff0c\u8fd8\u53ef\u4ee5\u4e0e\u6307\u9488\u4e00\u8d77\u4f7f\u7528\uff0c\u5f62\u6210 <strong>\u5e38\u91cf\u6307\u9488<\/strong> \u548c <strong>\u6307\u9488\u5e38\u91cf<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.1 \u5e38\u91cf\u6307\u9488<\/strong><\/h3>\n\n\n\n<p>\u5e38\u91cf\u6307\u9488\u6307\u7684\u662f<strong>\u6307\u9488\u6307\u5411\u7684\u5730\u5740\u4e0d\u53ef\u4fee\u6539<\/strong>\uff0c\u4f46\u6307\u9488\u672c\u8eab\u53ef\u4ee5\u6539\u53d8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    int x = 10;\n    int y = 20;\n    int* const ptr = &amp;x;  \/\/ \u5e38\u91cf\u6307\u9488\uff0c\u6307\u5411\u7684\u5730\u5740\u4e0d\u53ef\u53d8\n\n    cout &lt;&lt; \"*ptr = \" &lt;&lt; *ptr &lt;&lt; endl;\n\n    \/\/ ptr = &amp;y;  \/\/ \u9519\u8bef\uff1a\u5e38\u91cf\u6307\u9488\uff0c\u4e0d\u80fd\u6539\u53d8\u6307\u5411\u5730\u5740\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.2 \u6307\u9488\u5e38\u91cf<\/strong><\/h3>\n\n\n\n<p>\u6307\u9488\u5e38\u91cf\u6307\u7684\u662f<strong>\u6307\u9488\u672c\u8eab\u7684\u503c\u4e0d\u53ef\u4fee\u6539<\/strong>\uff0c\u4f46\u6307\u9488\u6307\u5411\u7684\u5730\u5740\u53ef\u4ee5\u6539\u53d8\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    int x = 10;\n    int y = 20;\n    const int* ptr = &amp;x;  \/\/ \u6307\u9488\u5e38\u91cf\uff0c\u6307\u9488\u7684\u503c\u4e0d\u53ef\u53d8\n\n    cout &lt;&lt; \"*ptr = \" &lt;&lt; *ptr &lt;&lt; endl;\n\n    ptr = &amp;y;  \/\/ \u53ef\u4ee5\u4fee\u6539\u6307\u5411\u7684\u5730\u5740\n    cout &lt;&lt; \"*ptr = \" &lt;&lt; *ptr &lt;&lt; endl;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3.3 \u5e38\u91cf\u6307\u9488\u4e0e\u6307\u9488\u5e38\u91cf<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nint main() {\n    int x = 10;\n    int y = 20;\n    const int* ptr1 = &amp;x;  \/\/ \u6307\u9488\u5e38\u91cf\n    int* const ptr2 = &amp;x;  \/\/ \u5e38\u91cf\u6307\u9488\n\n    \/\/ ptr1 = &amp;y;   \/\/ \u53ef\u4ee5\u4fee\u6539\u6307\u9488\u5730\u5740\n    \/\/ *ptr2 = 30;  \/\/ \u9519\u8bef\uff1a\u5e38\u91cf\u6307\u9488\u6307\u5411\u7684\u503c\u4e0d\u53ef\u4fee\u6539\n    cout &lt;&lt; \"*ptr1 = \" &lt;&lt; *ptr1 &lt;&lt; endl;\n    cout &lt;&lt; \"*ptr2 = \" &lt;&lt; *ptr2 &lt;&lt; endl;\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8f93\u51fa\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>*ptr1 = 10\n*ptr2 = 10<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. \u4f7f\u7528 <code>#define<\/code> \u5b8f\u5b9a\u4e49\u5e38\u91cf<\/strong><\/h2>\n\n\n\n<p>C++ \u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>#define<\/code> \u5b8f\u6765\u5b9a\u4e49\u5e38\u91cf\uff0c\u901a\u5e38\u7528\u4e8e<strong>\u5168\u5c40\u5e38\u91cf<\/strong>\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4.1 \u5b8f\u5b9a\u4e49\u5e38\u91cf\u793a\u4f8b<\/strong><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n#define PI 3.14159  \/\/ \u5b9a\u4e49\u5e38\u91cf PI\n\nint main() {\n    cout &lt;&lt; \"PI = \" &lt;&lt; PI &lt;&lt; endl;\n    \/\/ PI = 3.14;  \/\/ \u9519\u8bef\uff1a\u5b8f\u5b9a\u4e49\u5e38\u91cf\u65e0\u6cd5\u4fee\u6539\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e\uff1a<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>#define<\/code> \u662f\u9884\u5904\u7406\u6307\u4ee4\uff0c<strong>\u4e0d\u9700\u8981\u7c7b\u578b<\/strong>\uff0c\u76f4\u63a5\u66ff\u6362\u6587\u5b57\u3002<\/li>\n\n\n\n<li>\u5b8f\u5e38\u91cf\u5728\u7f16\u8bd1\u65f6<strong>\u8fdb\u884c\u6587\u672c\u66ff\u6362<\/strong>\uff0c\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u5730\u65b9\u3002<\/li>\n\n\n\n<li>\u4e0d\u50cf <code>const<\/code> \u548c <code>constexpr<\/code>\uff0c\u5b8f\u5e38\u91cf\u6ca1\u6709\u7c7b\u578b\u5b89\u5168\uff0c\u56e0\u6b64\u53ef\u80fd\u5bb9\u6613\u51fa\u73b0\u4e00\u4e9b\u6f5c\u5728\u95ee\u9898\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5. \u5e38\u91cf\u7684\u4f5c\u7528\u4e0e\u4f7f\u7528<\/strong><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u589e\u5f3a\u4ee3\u7801\u53ef\u8bfb\u6027<\/strong>\uff1a\u901a\u8fc7\u5e38\u91cf\u7684\u547d\u540d\u53ef\u4ee5\u4f7f\u4ee3\u7801\u66f4\u5177\u53ef\u8bfb\u6027\uff0c\u6613\u4e8e\u7406\u89e3\u5e38\u91cf\u7684\u542b\u4e49\u3002<\/li>\n\n\n\n<li><strong>\u9632\u6b62\u4fee\u6539<\/strong>\uff1a\u5e38\u91cf\u503c\u4e00\u65e6\u58f0\u660e\uff0c\u4e0d\u5141\u8bb8\u4fee\u6539\uff0c\u4fdd\u8bc1\u6570\u636e\u4e0d\u88ab\u610f\u5916\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>\u63d0\u9ad8\u4ee3\u7801\u5b89\u5168\u6027<\/strong>\uff1a\u5728\u591a\u5904\u4f7f\u7528\u5e38\u91cf\u800c\u4e0d\u76f4\u63a5\u4f7f\u7528\u6570\u5b57\uff08\u5982 3.14\uff09\uff0c\u53ef\u4ee5\u6709\u6548\u9632\u6b62\u9519\u8bef\u503c\u7684\u5f15\u5165\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>6. \u603b\u7ed3<\/strong><\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u5173\u952e\u5b57<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><strong>\u5e38\u91cf<\/strong><\/td><td><code>const<\/code><\/td><td>\u5141\u8bb8\u5b9a\u4e49\u5e38\u91cf\uff0c\u503c\u4e0d\u53ef\u4fee\u6539<\/td><\/tr><tr><td><strong>\u5e38\u91cf\u8868\u8fbe\u5f0f<\/strong><\/td><td><code>constexpr<\/code><\/td><td>\u7f16\u8bd1\u65f6\u5df2\u77e5\u7684\u5e38\u91cf\uff0c\u8981\u6c42\u5e38\u91cf\u8868\u8fbe\u5f0f<\/td><\/tr><tr><td><strong>\u5b8f\u5e38\u91cf<\/strong><\/td><td><code>#define<\/code><\/td><td>\u9884\u5904\u7406\u5668\u5b8f\u5b9a\u4e49\u5e38\u91cf\uff0c\u503c\u4e0d\u53ef\u4fee\u6539<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5e38\u91cf\u662f C++ \u7f16\u7a0b\u4e2d\u91cd\u8981\u7684\u5de5\u5177\uff0c\u5b83\u80fd\u5e2e\u52a9\u5f00\u53d1\u8005<strong>\u63d0\u9ad8\u4ee3\u7801\u5b89\u5168\u6027\u3001\u53ef\u8bfb\u6027\u548c\u53ef\u7ef4\u62a4\u6027<\/strong>\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u5e38\u91cf\uff0c\u80fd\u591f\u6709\u6548\u5730\u907f\u514d\u610f\u5916\u4fee\u6539\uff0c\u786e\u4fdd\u7a0b\u5e8f\u6570\u636e\u7684\u7a33\u5b9a\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e38\u91cf\u662f\u6307\u5728\u7a0b\u5e8f\u6267\u884c\u671f\u95f4\u5176\u503c\u4e0d\u4f1a\u6539\u53d8\u7684\u53d8\u91cf\u3002\u5e38\u91cf\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\uff0c\u5e76\u4e14\u53ef\u4ee5\u9632\u6b62\u610f\u5916\u4fee\u6539\u8fd9\u4e9b\u503c\u3002C+ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3242,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[81],"tags":[],"class_list":["post-3241","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3241","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3241"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3241\/revisions"}],"predecessor-version":[{"id":3243,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3241\/revisions\/3243"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3242"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3241"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3241"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3241"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}