{"id":221638,"date":"2021-07-13T08:59:55","date_gmt":"2021-07-13T00:59:55","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=221638"},"modified":"2021-06-28T11:00:32","modified_gmt":"2021-06-28T03:00:32","slug":"sister-linux-math","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/sister-linux-math.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdC++\u4e2d\u5e38\u91cf\u4e0e\u6307\u9488"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u5728C++\u5b66\u4e60\u4f7f\u7528\u8fc7\u7a0b\u4e2d,\u6bcf\u4e2a\u4eba\u90fd\u4e0d\u53ef\u907f\u514d\u5730\u4f7f\u7528\u6307\u9488,\u800c\u4e14\u90fd\u6216\u591a\u6216\u5c11\u7684\u63a5\u89e6\u8fc7\u5e38\u91cf\u6307\u9488\u6216\u6307\u9488\u5e38\u91cf,\u4f46\u662f\u5bf9\u8fd9\u4e24\u4e2a\u7684\u6982\u5ff5\u8fd8\u662f\u5f88\u5bb9\u6613\u641e\u7cca\u6d82\u7684,\u6240\u4ee5\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eC++\u4e2d\u5e38\u91cf\u4e0e\u6307\u9488\u7684\u76f8\u5173\u8d44\u6599,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u524d\u8a00
\n<\/strong><\/div>\n

\u548c\u6307\u9488\u8054\u7528\u6709\u4f77\u5fae\u5999\u7684\u5730\u65b9\uff0c\u4e4b\u524d\u4e00\u76f4\u4f77\u6655\uff0c\u73b0\u5728\u6765\u7ee7\u7eed\u7814\u7a76\u4e0b\u3002\u8bf8\u5982\u4ee5\u4e0b\uff1a<\/p>\n

\r\nint const;\r\nconst int;\r\nconst int* p;\r\nint const* p;\r\nint* const p;\r\nconst int* const p;<\/pre>\n

const \u662f\u4e00\u79cd\u5904\u7406\u7b26\u53f7\u5e38\u91cf\u7684\u65b9\u6cd5\uff0c\u4ee5 const \u58f0\u660e\u7684\u53d8\u91cf\uff0c\u4e00\u822c\u9996\u5b57\u6bcd\u5927\u5199\uff0c\u58f0\u660e\u4e4b\u540e\u65e0\u6cd5\u88ab\u4fee\u6539\u3002\u76f8\u6bd4\u4e8e define\uff0cconst \u4f1a\u663e\u5f0f\u7684\u6307\u5b9a\u7c7b\u578b\u3002\u9664\u5b9a\u4e49\u7b26\u53f7\u5916\uff0c\u4e00\u822c\u53ef\u7528\u4e8e\u51fd\u6570\u58f0\u660e\uff0c\u8868\u793a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u4efb\u4f55\u503c\uff1b\u7528\u4e8e\u53c2\u6570\uff0c\u8868\u793a\u51fd\u6570\u4e0d\u4f1a\u4fee\u6539\u53c2\u6570\uff1b\u751a\u81f3\u53ef\u4ee5\u7528\u4e8e\u58f0\u660e\u6570\u7ec4\u7684\u957f\u5ea6\u3002<\/p>\n

\u89e3\u8bfb<\/strong><\/div>\n

const\u9ed8\u8ba4\u4f5c\u7528\u4e8e\u5176\u5de6\u8fb9\u7684\u4e1c\u897f\u3002\u5de6\u8fb9\u6ca1\u4e1c\u897f\u7684\u60c5\u51b5\u4e0b\uff0c\u4f5c\u7528\u4e8e\u5176\u53f3\u8fb9\u7684\u4e1c\u897f\u30021<\/p>\n

const int* p\uff0c\u53ea\u6709\u53f3\u8fb9\u6709\u4e1c\u897f\uff0c\u4fee\u9970\u7684\u4e3a int\uff0c\u6240\u4ee5\u6570\u503c\u4e0d\u80fd\u88ab\u4fee\u6539\u3002\u5728\u4e0e * \u7ed3\u5408\uff0c\u610f\u601d\u662f*p\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5176\u5b83\u7684\u90fd\u53ef\u4ee5\u3002\u5373\u4e0d\u53ef\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5176\u6307\u5411\u7684\u5185\u5bb9\uff0c\u4f46\u53ef\u6539\u53d8\u6307\u9488\u672c\u8eab\u6240\u6307\u5411\u7684\u5730\u5740\u3002<\/p>\n

int const* p\uff0c\u5148\u4f5c\u7528\u4e8e\u5de6\u4fa7\u7684 int \u4e3a int const\uff0c\u5728\u53e0\u52a0\u4e0a\u53f3\u4fa7\u7684 *\uff0c\u6240\u4ee5\u4fee\u9970\u7684\u4e3a int* p\uff0c\u6240\u4ee5\uff0c*p\u4e0d\u80fd\u88ab\u4fee\u6539\uff0c\u5176\u5b83\u7684\u90fd\u53ef\u4ee5\u3002\u5373\u4e0d\u53ef\u901a\u8fc7\u8be5\u6307\u9488\u6539\u53d8\u5176\u6307\u5411\u7684\u5185\u5bb9\uff0c\u4f46\u53ef\u6539\u53d8\u6307\u9488\u672c\u8eab\u6240\u6307\u5411\u7684\u5730\u5740\u3002\u4e5f\u5c31\u662f\uff0c\u548c\u4e0a\u9762\u7684\u662f\u4e00\u6837\u7684\u3002<\/p>\n

int* const p\uff0c\u5de6\u8fb9\u662f *\uff0c\u6240\u4ee5 const \u4f5c\u7528\u4e8e\u6307\u9488\uff0c\u6307\u5411\u4e00\u4e2a int \u53d8\u91cf\u3002\u5373\u4e0d\u53ef\u4ee5\u4fee\u6539 p\uff0c\u4f46\u53ef\u4ee5\u4fee\u6539 *p\uff0c\u5373\u4e0d\u53ef\u6539\u53d8\u6307\u5411\u7684\u5730\u5740\u3002<\/p>\n

const int* const p\uff0c\u5bf9\u4e8e\u7b2c\u4e00\u4e2a const\uff0c\u5de6\u8fb9\u6ca1\u4e1c\u897f\uff0c\u4fee\u9970\u53f3\u8fb9\u7684 int\uff0c\u6307\u5411\u7684\u503c\u4e0d\u80fd\u4fee\u6539\uff1b\u5bf9\u4e8e\u7b2c\u4e8c\u4e2a const \u4fee\u9970 *\uff0c\u6307\u9488\u4e0d\u80fd\u4fee\u6539\u3002\u5373\u4e0d\u53ef\u6539\u53d8\u6307\u9488\u672c\u8eab\u6240\u6307\u5411\u7684\u5730\u5740\uff0c\u4e5f\u4e0d\u53ef\u901a\u8fc7\u6307\u9488\u6539\u53d8\u5176\u6307\u5411\u7684\u5185\u5bb9\u3002\u540c int const* const p\u3002<\/p>\n

int const* const* p\uff0c\u7b2c\u4e00\u4e2a const \u4fee\u9970 int\uff0c\u7b2c\u4e8c\u4e2a const \u4fee\u9970\u7b2c\u4e00\u4e2a *\uff0c\u4e5f\u5c31\u662f\uff0c\u6307\u5411 const int* const p \u7684\u6307\u9488\uff0c\u6700\u540e\u4e00\u4e2a * \u6ca1\u6709\u88ab\u4fee\u9970\uff0c\u56e0\u6b64\u53ef\u4ee5\u6307\u5411\u5176\u5b83\u53d8\u91cf\u3002int const* const* const \u5c31\u4e0d\u53ef\u4ee5\u4e86\u3002\u4e4b\u540e\u518d\u51fa\u73b0\u6b64\u7c7b\u60c5\u51b5\uff0c\u4e5f\u53ef\u4ee5\u6162\u6162\u5206\u6790\u6ee1\u5929\u98de\u7684 const \u548c\u6307\u9488\u3002<\/p>\n

\u4e00\u4e9b\u4f8b\u5b50<\/strong><\/div>\n

\u4e3a\u4e86\u66f4\u597d\u7684\u7406\u89e3\u4e0a\u8ff0\u5185\u5bb9\uff0c\u8fd9\u91cc\u6765\u4e3e\u4e00\u4e9b\u4f8b\u5b50\u3002\u5e38\u89c1\u7684\u4e00\u822c\u6709\u4e24\u79cd\u9009\u62e9\uff1a<\/p>\n

\u5e38\u6307\u9488\u6307\u5411\u4e00\u4e2a\u53d8\u91cf\uff0c\u9632\u6b62\u4fee\u6539\u6307\u9488\u4fee\u6539\u53d8\u91cf\u503c<\/p>\n

\u5e38\u6307\u9488\u6307\u5411\u4e00\u4e2a\u5e38\u91cf<\/p>\n

\u975e\u5e38\u6307\u9488\u6307\u5411\u5e38\u91cf\uff08\u9519\u8bef\uff09<\/p>\n

\u5148\u770b\u7b2c\u4e00\u79cd\u60c5\u51b5\uff1a\u89e3\u5f15\u7528\u53ea\u662f\u53d6\u51fa\u6307\u5411\u5185\u5b58\u533a\u57df\u7684\u503c\uff0c\u56e0\u6b64\u6307\u5411\u5185\u5b58\u533a\u57df\u7684\u503c\u662f\u53ef\u4ee5\u76f4\u63a5\u4fee\u6539\u7684\uff0c\u4f46\u4e0d\u80fd\u901a\u8fc7\u6307\u9488\u4fee\u6539\u3002<\/p>\n

\r\nint main (){\r\n    int a{34};\r\n    const int *p = &a;\r\n    \/\/ *p \u4e3a const\uff0c\u4e0d\u80fd\u88ab\u4fee\u6539\r\n    \/\/ \u9519\u8bef\r\n    \/\/ *p ++;\r\n    \/\/ p \u6307\u5411\u7684\u4e0d\u662f\u5e38\u91cf\uff0c\u56e0\u6b64\uff0c\u53ef\u4ee5\u4fee\u6539 a\r\n    a ++;\r\n    std::cout << *p << std::endl;\r\n     \r\n    int b{12};\r\n    p = &b;\r\n    std::cout << *p << std::endl;\r\n    return 0;\r\n}<\/pre>\n

\u5bf9\u4e8e\u7b2c\u4e8c\u79cd\u60c5\u51b5\uff1a\u4e0d\u80fd\u4fee\u6539\u53d8\u91cf\uff0c\u4e5f\u4e0d\u80fd\u4fee\u6539\u5e38\u91cf\u3002<\/p>\n

\r\nint main (){\r\n \r\n    const int a{34};\r\n    \/\/ *p \u4e3a const\uff0c\u4e0d\u80fd\u88ab\u4fee\u6539\uff0ca \u4e5f\u4e0d\u80fd\u88ab\u4fee\u6539\r\n    const int *p = &a;\r\n    std::cout << *p << std::endl;\r\n \r\n    int b{12};\r\n    p = &b;\r\n    std::cout << *p << std::endl;\r\n \r\n    return 0;\r\n}<\/pre>\n

\u5bf9\u4e8e\u7b2c\u4e09\u79cd\u60c5\u51b5\uff1a\u4fee\u6539\u6307\u9488\u6765\u4fee\u6539\u5e38\u91cf\u4f1a\u663e\u5f97\u4f77\u8352\u8c2c\uff0c\u56e0\u6b64\u7f16\u8bd1\u4f1a\u76f4\u63a5\u62a5\u9519\uff1a<\/p>\n

\r\nint main (){\r\n    const int a{34};\r\n    \/\/ error: invalid conversion from 'const int*' to 'int*'\r\n    int *p = &a;\r\n    *p ++;\r\n    std::cout << *p ;\r\n    return 0;\r\n}<\/pre>\n
\u4e8c\u7ea7\u6307\u9488<\/strong><\/div>\n

\u4e4b\u524d\u8bf4\u5230\uff0c\u5e38\u6307\u9488\u53ef\u4ee5\u6307\u5411\u53d8\u91cf\uff0c\u4f46\u662f\u6d89\u53ca\u4e8c\u7ea7\u6307\u9488\u540e\uff0c\u60c5\u51b5\u53c8\u4f1a\u53d1\u751f\u9006\u8f6c\u3002<\/p>\n

\r\nint main (){\r\n \r\n    const int a{12};\r\n    const int** p1;\r\n    int* p2;\r\n \r\n    \/\/ error: invalid conversion from 'int**' to 'const int**'\r\n    p1 = &p2;\r\n    *p1 = &a;\r\n \r\n    *p2 = 10;\r\n \r\n    return 0;\r\n}<\/pre>\n

\u5982\u679c\u4e0a\u8ff0\u4ee3\u7801\u901a\u8fc7\uff0c\u90a3\u4e48\u5b8c\u5168\u53ef\u4ee5\u901a\u8fc7 p2 \u6307\u9488\u4fee\u6539\u5e38\u91cf\u3002\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5f97\u5230\u4ee5\u4e0b\u7ed3\u8bba\uff1a<\/p>\n

\u82e5\u6570\u636e\u7c7b\u578b\u672c\u8eab\u4e0d\u662f\u6307\u9488\uff0c\u53ef\u4ee5\u5c06 const \u6570\u636e\u6216\u975e const \u6570\u636e\u7684\u5730\u5740\u8d4b\u7ed9\u6307\u5411 const \u7684\u6307\u9488\uff0c\u4f46\u6307\u9488\u53ef\u4ee5\u4fee\u6539\uff0c\u6307\u5411\u522b\u7684\u503c\u3002\u56e0\u6b64\uff0cconst \u4fee\u9970\u7684\u6570\u7ec4\u4e0d\u80fd\u4f20\u53c2\u7ed9\u975e\u5e38\u91cf\u6307\u9488\u3002<\/p>\n

\u5982\u679c\u6570\u636e\u7c7b\u578b\u662f\u6307\u9488\uff0c\u975e const \u6570\u636e\u7684\u5730\u5740\u53ea\u80fd\u8d4b\u503c\u7ed9\u975e const \u6307\u9488\uff0c\u5982\u4e8c\u7ea7\u6307\u9488\u4e2d\uff0cp1 = &p2 \u662f\u9519\u8bef\u7684\u3002<\/p>\n

\u603b\u7ed3<\/strong><\/div>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eC++\u4e2d\u5e38\u91cf\u4e0e\u6307\u9488\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u548c\u6307\u9488\u8054\u7528\u6709\u4f77\u5fae\u5999\u7684\u5730\u65b9\uff0c\u4e4b\u524d\u4e00\u76f4\u4f77\u6655\uff0c\u73b0\u5728\u6765\u7ee7\u7eed\u7814\u7a76\u4e0b\u3002\u8bf8\u5982\u4ee5\u4e0b\uff1a int const; const int […]<\/p>\n","protected":false},"author":1482,"featured_media":52821,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-221638","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221638","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1482"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=221638"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221638\/revisions"}],"predecessor-version":[{"id":221641,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/221638\/revisions\/221641"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/52821"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=221638"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=221638"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=221638"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}