\u56db\u3001const\u4fee\u9970\u7c7b\u6210\u5458\u51fd\u6570<\/strong><\/div>\nconst \u4fee\u9970\u7c7b\u6210\u5458\u51fd\u6570\uff0c\u5176\u76ee\u7684\u662f\u9632\u6b62\u6210\u5458\u51fd\u6570\u4fee\u6539\u88ab\u8c03\u7528\u5bf9\u8c61\u7684\u503c\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u60f3\u4fee\u6539\u4e00\u4e2a\u8c03\u7528\u5bf9\u8c61\u7684\u503c\uff0c\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u5e94\u5f53\u58f0\u660e\u4e3a const \u6210\u5458\u51fd\u6570\u3002<\/p>\n
\u6ce8\u610f\uff1aconst \u5173\u952e\u5b57\u4e0d\u80fd\u4e0e static \u5173\u952e\u5b57\u540c\u65f6\u4f7f\u7528\uff0c\u56e0\u4e3a static \u5173\u952e\u5b57\u4fee\u9970\u9759\u6001\u6210\u5458\u51fd\u6570\uff0c\u9759\u6001\u6210\u5458\u51fd\u6570\u4e0d\u542b\u6709 this \u6307\u9488\uff0c\u5373\u4e0d\u80fd\u5b9e\u4f8b\u5316\uff0cconst \u6210\u5458\u51fd\u6570\u5fc5\u987b\u5177\u4f53\u5230\u67d0\u4e00\u5b9e\u4f8b\u3002<\/p>\n
\u4e0b\u9762\u7684 get_cm()const; \u51fd\u6570\u7528\u5230\u4e86 const \u6210\u5458\u51fd\u6570\uff1a<\/p>\n
\u5b9e\u4f8b<\/strong><\/p>\n#include\r\n \r\nusing namespace std;\r\n \r\nclass Test\r\n{\r\npublic:\r\n Test(){}\r\n Test(int _m):_cm(_m){}\r\n int get_cm()const\r\n {\r\n return _cm;\r\n }\r\n \r\nprivate:\r\n int _cm;\r\n};\r\n \r\n \r\n \r\nvoid Cmf(const Test& _tt)\r\n{\r\n cout<<_tt.get_cm();\r\n}\r\n \r\nint main(void)\r\n{\r\n Test t(8);\r\n Cmf(t);\r\n system(\"pause\");\r\n return 0;\r\n}<\/iostream><\/pre>\n\u5982\u679c get_cm() \u53bb\u6389 const \u4fee\u9970\uff0c\u5219 Cmf \u4f20\u9012\u7684 const _tt \u5373\u4f7f\u6ca1\u6709\u6539\u53d8\u5bf9\u8c61\u7684\u503c\uff0c\u7f16\u8bd1\u5668\u4e5f\u8ba4\u4e3a\u51fd\u6570\u4f1a\u6539\u53d8\u5bf9\u8c61\u7684\u503c\uff0c\u6240\u4ee5\u6211\u4eec\u5c3d\u91cf\u6309\u7167\u8981\u6c42\u5c06\u6240\u6709\u7684\u4e0d\u9700\u8981\u6539\u53d8\u5bf9\u8c61\u5185\u5bb9\u7684\u51fd\u6570\u90fd\u4f5c\u4e3a const \u6210\u5458\u51fd\u6570\u3002<\/p>\n
\u5982\u679c\u6709\u4e2a\u6210\u5458\u51fd\u6570\u60f3\u4fee\u6539\u5bf9\u8c61\u4e2d\u7684\u67d0\u4e00\u4e2a\u6210\u5458\u600e\u4e48\u529e\uff1f\u8fd9\u65f6\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528 mutable \u5173\u952e\u5b57\u4fee\u9970\u8fd9\u4e2a\u6210\u5458\uff0cmutable \u7684\u610f\u601d\u4e5f\u662f\u6613\u53d8\u7684\uff0c\u5bb9\u6613\u6539\u53d8\u7684\u610f\u601d\uff0c\u88ab mutable \u5173\u952e\u5b57\u4fee\u9970\u7684\u6210\u5458\u53ef\u4ee5\u5904\u4e8e\u4e0d\u65ad\u53d8\u5316\u4e2d\uff0c\u5982\u4e0b\u9762\u7684\u4f8b\u5b50\u3002<\/p>\n
\u5b9e\u4f8b<\/strong><\/p>\n#include\r\nusing namespace std;\r\nclass Test\r\n{\r\npublic:\r\n Test(int _m,int _t):_cm(_m),_ct(_t){}\r\n void Kf()const\r\n {\r\n ++_cm; \/\/ \u9519\u8bef\r\n ++_ct; \/\/ \u6b63\u786e\r\n }\r\nprivate:\r\n int _cm;\r\n mutable int _ct;\r\n};\r\n \r\nint main(void)\r\n{\r\n Test t(8,7);\r\n return 0;\r\n}<\/iostream><\/pre>\n\u8fd9\u91cc\u6211\u4eec\u5728 Kf()const \u4e2d\u901a\u8fc7 ++_ct; \u4fee\u6539 _ct \u7684\u503c\uff0c\u4f46\u662f\u901a\u8fc7 ++_cm \u4fee\u6539 _cm \u5219\u4f1a\u62a5\u9519\u3002\u56e0\u4e3a ++_cm \u6ca1\u6709\u7528 mutable \u4fee\u9970\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"
C++ const \u5141\u8bb8\u6307\u5b9a\u4e00\u4e2a\u8bed\u4e49\u7ea6\u675f\uff0c\u7f16\u8bd1\u5668\u4f1a\u5f3a\u5236\u5b9e\u65bd\u8fd9\u4e2a\u7ea6\u675f\uff0c\u5141\u8bb8\u7a0b\u5e8f\u5458\u544a\u8bc9\u7f16\u8bd1\u5668\u67d0\u503c\u662f\u4fdd\u6301\u4e0d\u53d8\u7684\u3002\u5982\u679c […]<\/p>\n","protected":false},"author":1470,"featured_media":199529,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-199525","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199525","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/1470"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=199525"}],"version-history":[{"count":4,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199525\/revisions"}],"predecessor-version":[{"id":199542,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/199525\/revisions\/199542"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/199529"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=199525"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=199525"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=199525"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}