{"id":7313,"date":"2024-04-07T23:01:01","date_gmt":"2024-04-07T15:01:01","guid":{"rendered":""},"modified":"2024-04-07T23:01:01","modified_gmt":"2024-04-07T15:01:01","slug":"C++\u6838\u5fc3\u51c6\u5219Con.4:\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u6784\u5efa\u4e4b\u540e\u503c\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f7f\u7528const\u5b9a\u4e49\u5b83","status":"publish","type":"post","link":"https:\/\/mushiming.com\/7313.html","title":{"rendered":"C++\u6838\u5fc3\u51c6\u5219Con.4:\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u6784\u5efa\u4e4b\u540e\u503c\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f7f\u7528const\u5b9a\u4e49\u5b83"},"content":{"rendered":"
Con.4:\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u6784\u5efa\u4e4b\u540e\u503c\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f7f\u7528const\u5b9a\u4e49\u5b83<\/strong><\/p>\n Prevent surprises from unexpectedly changed object values.<\/p>\n \u9632\u6b62\u5bf9\u8c61\u503c\u88ab\u610f\u5916\u4fee\u6539\u7684\u60c5\u51b5\u3002<\/p>\n <\/p>\n Example\uff08\u793a\u4f8b\uff09<\/strong><\/p>\n As x is not const, we must assume that it is modified somewhere in the loop.<\/p>\n \u7531\u4e8ex\u6ca1\u6709\u5b9a\u4e49\u4e3a\u5e38\u91cf\u7c7b\u578b\uff0c\u6211\u4eec\u5fc5\u987b\u5047\u8bbe\u5b83\u53ef\u80fd\u5728\u5faa\u73af\u7684\u67d0\u5904\u88ab\u4fee\u6539\u3002<\/p>\n <\/p>\n Enforcement\uff08\u5b9e\u65bd\u5efa\u8bae\uff09<\/strong><\/p>\n Flag unmodified non-const variables.<\/p>\n<\/li>\n \u6807\u8bb0\u4e0d\u4f1a\u88ab\u4fee\u6539\u7684\u975e\u5e38\u91cf\u3002<\/p>\n <\/p>\n<\/li>\n<\/ul>\n \u539f\u6587\u94fe\u63a5<\/strong><\/p>\n https:\/\/github.com\/isocpp\/CppCoreGuidelines\/blob\/master\/CppCoreGuidelines.md#con4-use-const-to-define-objects-with-values-that-do-not-change-after-construction<\/p>\n <\/p>\n \u65b0\u4e66\u4ecb\u7ecd<\/strong><\/p>\n \u300a\u5b9e\u6218Python\u8bbe\u8ba1\u6a21\u5f0f\u300b\u4e00\u4e66\u662f\u4f5c\u8005\u6700\u8fd1\u51fa\u7248\u7684\u65b0\u4e66\uff0c\u62dc\u6258\u591a\u591a\u5173\u6ce8\uff01<\/p>\n <\/p>\n \u672c\u4e66\u5229\u7528Python \u7684\u6807\u51c6GUI \u5de5\u5177\u5305tkinter\uff0c\u901a\u8fc7\u53ef\u6267\u884c\u7684\u793a\u4f8b\u5bf923 \u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u9010\u4e2a\u8fdb\u884c\u8bf4\u660e\u3002\u8fd9\u6837\u4e00\u65b9\u9762\u53ef\u4ee5\u4f7f\u8bfb\u8005\u4e86\u89e3\u771f\u5b9e\u7684\u8f6f\u4ef6\u5f00\u53d1\u5de5\u4f5c\u4e2d\u6bcf\u4e2a\u8bbe\u8ba1\u6a21\u5f0f\u7684\u8fd0\u7528\u573a\u666f\u548c\u60f3\u8981\u89e3\u51b3\u7684\u95ee\u9898\uff1b\u53e6\u4e00\u65b9\u9762\u901a\u8fc7\u5bf9\u8fd9\u4e9b\u95ee\u9898\u7684\u89e3\u51b3\u8fc7\u7a0b\u8fdb\u884c\u8bf4\u660e\uff0c\u8ba9\u8bfb\u8005\u660e\u767d\u5728\u7f16\u5199\u4ee3\u7801\u65f6\u5982\u4f55\u5224\u65ad\u4f7f\u7528\u8bbe\u8ba1\u6a21\u5f0f\u7684\u5229\u5f0a\uff0c\u5e76\u5408\u7406\u8fd0\u7528\u8bbe\u8ba1\u6a21\u5f0f\u3002<\/p>\n \u5bf9\u8bbe\u8ba1\u6a21\u5f0f\u611f\u5174\u8da3\u800c\u4e14\u5e0c\u671b\u968f\u5b66\u968f\u7528\u7684\u8bfb\u8005\u901a\u8fc7\u672c\u4e66\u53ef\u4ee5\u5feb\u901f\u8de8\u8d8a\u4ece\u7406\u89e3\u5230\u8fd0\u7528\u7684\u95e8\u69db\uff1b\u5e0c\u671b\u5b66\u4e60Python GUI \u7f16\u7a0b\u7684\u8bfb\u8005\u53ef\u4ee5\u5c06\u672c\u4e66\u4e2d\u7684\u793a\u4f8b\u4f5c\u4e3a\u8bbe\u8ba1\u548c\u5f00\u53d1\u7684\u53c2\u8003\uff1b\u4f7f\u7528Python \u8bed\u8a00\u8fdb\u884c\u56fe\u50cf\u5206\u6790\u3001\u6570\u636e\u5904\u7406\u5de5\u4f5c\u7684\u8bfb\u8005\u53ef\u4ee5\u76f4\u63a5\u4ee5\u672c\u4e66\u4e2d\u7684\u793a\u4f8b\u4e3a\u57fa\u7840\uff0c\u8fc5\u901f\u6784\u5efa\u81ea\u5df1\u7684\u7cfb\u7edf\u67b6\u6784\u3002<\/p>\n <\/p>\n <\/p>\n \u89c9\u5f97\u672c\u6587\u6709\u5e2e\u52a9\uff1f\u8bf7\u5206\u4eab\u7ed9\u66f4\u591a\u4eba\u3002<\/p>\n \u5173\u6ce8\u5fae\u4fe1\u516c\u4f17\u53f7\u3010\u9762\u5411\u5bf9\u8c61\u601d\u8003\u3011\u8f7b\u677e\u5b66\u4e60\u6bcf\u4e00\u5929\uff01<\/p>\n \u9762\u5411\u5bf9\u8c61\u5f00\u53d1\uff0c\u9762\u5411\u5bf9\u8c61\u601d\u8003\uff01<\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":"C++\u6838\u5fc3\u51c6\u5219Con.4:\u5982\u679c\u4e00\u4e2a\u5bf9\u8c61\u5728\u6784\u5efa\u4e4b\u540e\u503c\u4e0d\u4f1a\u6539\u53d8\uff0c\u4f7f\u7528const\u5b9a\u4e49\u5b83Con.4:Useconsttodefineobjectswithvaluesthatdonotcha...","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"_links":{"self":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7313"}],"collection":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/comments?post=7313"}],"version-history":[{"count":0,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/posts\/7313\/revisions"}],"wp:attachment":[{"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/media?parent=7313"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/categories?post=7313"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mushiming.com\/wp-json\/wp\/v2\/tags?post=7313"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}} <\/h4>\n
Reason\uff08\u539f\u56e0\uff09<\/strong><\/h4>\n
void f()\n{\n int x = 7;\n const int y = 9;\n\n for (;;) {\n \/\/ ...\n }\n \/\/ ...\n}\n<\/code><\/pre>\n
\n
\n