{"id":240,"date":"2025-08-06T17:26:18","date_gmt":"2025-08-06T09:26:18","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=240"},"modified":"2025-11-24T16:39:15","modified_gmt":"2025-11-24T08:39:15","slug":"c-c%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=240","title":{"rendered":"c\/c++\u7c7b\u578b\u8f6c\u6362"},"content":{"rendered":"\n<p>gitee\u4ed3\u5e93\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/c++Type%20conversion\">https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/c++Type%20conversion<\/a><\/p>\n\n\n\n<p><strong>1. C\u4e2d\u7684\u7c7b\u578b\u8f6c\u6362 <\/strong><\/p>\n\n\n\n<p>    \u5728C\u8bed\u2f94\u4e2d\uff0c\u5982\u679c\u8d4b\u503c\u8fd0\u7b97\u7b26\u5de6\u53f3\u4e24\u4fa7\u7c7b\u578b\u4e0d\u540c\uff0c\u6216\u8005\u5f62\u53c2\u4e0e\u5b9e\u53c2\u7c7b\u578b\u4e0d\u5339\u914d\uff0c\u6216\u8005\u8fd4\u56de\u503c\u7c7b\u578b\u4e0e\u63a5\u6536\u8fd4\u56de\u503c\u7c7b\u578b\u4e0d\u2f00\u81f4\u65f6\u7b49\u573a\u666f\uff0c\u5c31\u9700\u8981\u53d1\u2f63\u7c7b\u578b\u8f6c\u5316\uff0cC\u8bed\u2f94\u4e2d\u603b\u5171\u6709\u4e24\u79cd\u5f62\u5f0f\u7684\u7c7b\u578b\u8f6c\u6362\uff1a\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u548c\u663e\u5f0f\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u3002<\/p>\n\n\n\n<p>    1. \u9690\u5f0f\u7c7b\u578b\u8f6c\u5316\uff1a\u7f16\u8bd1\u5668\u5728\u7f16\u8bd1\u9636\u6bb5\u2f83\u52a8\u8fdb\u2f8f\uff0c\u80fd\u8f6c\u5c31\u8f6c\uff0c\u4e0d\u80fd\u8f6c\u5c31\u7f16\u8bd1\u5931\u8d252<\/p>\n\n\n\n<p>    2.\u663e\u5f0f\u5f3a\u5236\u7c7b\u578b\u8f6c\u5316\uff1a\u9700\u8981\u2f64\u2f3e\u2f83\u53bb\u663e\u2f70\u5728\u53d8\u91cf\u524d\u2f64\u62ec\u53f7\u6307\u5b9a\u8981\u8f6c\u6362\u7684\u7c7b\u578b\u2022<\/p>\n\n\n\n<p>    \u5e76\u4e0d\u662f\u4efb\u610f\u7c7b\u578b\u4e4b\u524d\u90fd\u2f40\u6301\u8f6c\u6362\uff0c\u4e24\u4e2a\u7c7b\u578b\u2f40\u6301\u8f6c\u6362\u9700\u8981\u6709\u2f00\u5b9a\u5173\u8054\u6027\uff0c\u4e5f\u5c31\u662f\u8bf4\u8f6c\u6362\u540e\u8981\u6709\u2f00\u5b9a\u7684\u610f\u4e49\uff0c\u4e24\u4e2a\u6beb\u2f46\u5173\u8054\u7684\u7c7b\u578b\u662f\u4e0d\u2f40\u6301\u8f6c\u6362\u7684\u3002<\/p>\n\n\n\n<p>\u5982\u4e0b\u9762\uff1a<\/p>\n\n\n\n<p>1.\u9690\u5f0f\u7c7b\u578b\u8f6c\u5316\u4e3b\u8981\u662f:\u6d6e\u70b9\u6570\u548c\u6574\u5f62\u4e4b\u95f4\uff0c\u6d6e\u70b9\u6570\u548c\u6d6e\u70b9\u6570\uff0c\u6574\u6570\u548c\u6574\u6570<\/p>\n\n\n\n<p>2.\u663e\u793a\u7c7b\u578b\u8f6c\u5316\u4e3b\u8981\u662f\uff1a\u4e3b\u8981\u53d1\u751f\u5728\u6307\u9488\u548c\u6574\u5f62\u4e4b\u95f4\uff0c\u6307\u9488\u548c\u6307\u9488\u4e4b\u95f4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n\tint i = 1;\n\tdouble d = i;\n\tint* p = &i;\n\tint addr = (int)p;\n\t\/\/malloc\u8fd4\u56de\u7684\u662fvoid*,\u88ab\u5f3a\u8f6c\u6210int*\n\t\/\/int* ptr = (int*)malloc(8);\n\tcout << sizeof(*(void**)p) << endl;\n\tcout << p << endl;\n\tcout << addr;\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>2.c++\u4e2d\u7684\u7c7b\u578b\u8f6c\u5316<\/strong><\/p>\n\n\n\n<p>    1.C++\u517c\u5bb9C\uff0c\u6240\u4ee5C\u2f40\u6301\u7684\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u548c\u663e\u5f0f\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362C++\u90fd\u2f40\u6301\u3002<\/p>\n\n\n\n<p>    2.C++\u8fd8\u2f40\u6301\u5185\u7f6e\u7c7b\u578b\u5230\u2f83\u5b9a\u4e49\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u5185\u7f6e\u7c7b\u578b\u8f6c\u6210\u2f83\u5b9a\u4e49\u7c7b\u578b\u9700\u8981\u6784\u9020\u51fd\u6570\u7684\u2f40\u6301\uff0c\u2f83\u5b9a\u4e49\u7c7b\u578b\u8f6c\u6210\u5185\u7f6e\u7c7b\u578b\uff0c\u9700\u8981\u2f00\u4e2aoperator \u7c7b\u578b()\u7684\u51fd\u6570\u53bb\u2f40\u6301\u3002 <\/p>\n\n\n\n<p>    3.C++\u8fd8\u2f40\u6301\u2f83\u5b9a\u4e49\u7c7b\u578b\u5230\u2f83\u5b9a\u4e49\u7c7b\u578b\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u9700\u8981\u5bf9\u5e94\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u2f40\u6301\u5373\u53ef\uff0c\u2f50\u5982A\u7c7b\u578b\u5bf9\u8c61\u60f3\u8f6c\u6210B\u7c7b\u578b\uff0c\u5219\u2f40\u6301\u2f00\u4e2a\u5f62\u53c2\u4e3aA\u7c7b\u578b\u7684B\u6784\u9020\u51fd\u6570\u5373\u53ef\u2f40\u6301\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include <iostream>\nusing namespace std;\n\/\/ \u5185\u7f6e\u7c7b\u578b\u548c\u2f83\u5b9a\u4e49\u7c7b\u578b\u4e4b\u95f4\n\/\/ 1\u3001\u2f83\u5b9a\u4e49\u7c7b\u578b = \u5185\u7f6e\u7c7b\u578b ->\u6784\u9020\u51fd\u6570\u2f40\u6301\n\/\/ 2\u3001\u5185\u7f6e\u7c7b\u578b = \u2f83\u5b9a\u4e49\u7c7b\u578b ->operator \u5185\u7f6e\u7c7b\u578b \u2f40\u6301\nclass A\n{\npublic:\n\t\/\/ \u6784\u9020\u51fd\u6570\u52a0\u4e0aexplicit\u5c31\u4e0d\u2f40\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u4e86\n\t\/\/explicit A(int a)\n\tA(int a)\n\t\t:_a1(a)\n\t\t, _a2(a)\n\t{}\n\tA(int a1, int a2)\n\t\t:_a1(a1)\n\t\t, _a2(a2)\n\t{}\n\t\/\/ \u52a0\u4e0aexplicit\u5c31\u4e0d\u2f40\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u4e86\n\t\/\/ explicit operator int()\n\toperator int() const\n\t{\n\t\treturn _a1 + _a2;\n\t}\nprivate:\n\tint _a1 = 1;\n\tint _a2 = 1;\n};\nclass B\n{\npublic:\n\tB(int b)\n\t\t:_b1(b)\n\t{}\n\t\/\/ \u2f40\u6301A\u7c7b\u578b\u5bf9\u8c61\u8f6c\u6362\u4e3aB\u7c7b\u578b\u5bf9\u8c61\n\tB(const A& aa)\n\t\t:_b1(aa)\n\t{}\nprivate:\n\tint _b1 = 1;\n};\nint main()\n{\n\t\/\/ \u5355\u53c2\u6570\u7684\u8f6c\u6362\n\tstring s1 = \"1111111\";\n\tA aa1 = 1;\n\tA aa2 = (A)1;\n\t\/\/ \u591a\u53c2\u6570\u7684\u8f6c\u6362\n\tA aa3 = { 2,2 };\n\tconst A& aa4 = { 2,2 };\n\tint z = aa1.operator int();\n\tint x = aa1;\n\tint y = (int)aa2;\n\tcout << x << endl;\n\tcout << y << endl;\n\tcout << z << endl;\n\tstd::shared_ptr<int> foo;\n\tstd::shared_ptr<int> bar(new int(34));\n\t\/\/if (foo.operator bool())\n\tif (foo)\n\t\tstd::cout << \"foo points to \" << *foo << '\\n';\n\telse\n\t\tstd::cout << \"foo is null\\n\";\n\tif (bar)\n\t\tstd::cout << \"bar points to \" << *bar << '\\n';\n\telse\n\t\tstd::cout << \"bar is null\\n\";\n\t\/\/ A\u7c7b\u578b\u5bf9\u8c61\u9690\u5f0f\u8f6c\u6362\u4e3aB\u7c7b\u578b\n\tB bb1 = aa1;\n\tB bb2(2);\n\tbb2 = aa1;\n\tconst B&#038; ref1 = aa1;\n\treturn 0;\n}<\/code><\/pre>\n\n\n\n<p><strong>3.\u7c7b\u578b\u5b89\u5168<\/strong> <\/p>\n\n\n\n<p>    1. \u7c7b\u578b\u5b89\u5168\u662f\u6307\u7f16\u7a0b\u8bed\u2f94\u5728\u7f16\u8bd1\u548c\u8fd0\u2f8f\u65f6\u63d0\u4f9b\u4fdd\u62a4\u673a\u5236\uff0c\u907f\u514d\u2fae\u6cd5\u7684\u7c7b\u578b\u8f6c\u6362\u548c\u64cd\u4f5c\uff0c\u5bfc\u81f4\u51fa\u73b0\u2f00\u4e2a\u5185\u5b58\u8bbf\u95ee\u9519\u8bef\u7b49\uff0c\u4ece\u2f7d\u51cf\u5c11\u7a0b\u5e8f\u8fd0\u2f8f\u65f6\u7684\u9519\u8bef<\/p>\n\n\n\n<p>    2. C\u8bed\u2f94\u4e0d\u662f\u7c7b\u578b\u5b89\u5168\u7684\u8bed\u2f94\uff0cC\u8bed\u2f94\u5141\u8bb8\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u2f00\u4e9b\u7279\u6b8a\u60c5\u51b5\u4e0b\u5c31\u4f1a\u5bfc\u81f4\u8d8a\u754c\u8bbf\u95ee\u7684\u5185\u5b58\u9519\u8bef\uff0c\u5176\u6b21\u4e0d\u5408\u7406\u7684\u4f7f\u2f64\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\u4e5f\u4f1a\u5bfc\u81f4\u95ee\u9898\uff0c\u2f50\u5982\u2f00\u4e2aint*\u7684\u6307\u9488\u5f3a\u8f6c\u6210double*\u8bbf\u95ee\u5c31\u4f1a\u51fa\u73b0\u8d8a\u754c\u3002 <\/p>\n\n\n\n<p>    3. C++\u517c\u5bb9C\u8bed\u2f94\uff0c\u2f40\u6301\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u548c\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362\uff0cC++\u4e5f\u4e0d\u662f\u7c7b\u578b\u5b89\u5168\u7684\u8bed\u2f94\uff0cC++\u63d0\u51fa4\u4e2a\u663e\u2f70\u7684\u547d\u540d\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362static_cast\/reinterpret_cast\/const_cast\/dynamic_cast\u5c31\u662f\u4e3a\u4e86\u8ba9\u7c7b\u578b\u8f6c\u6362\u76f8\u5bf9\u2f7d\u2f94\u66f4\u5b89\u5168\u3002<\/p>\n\n\n\n<p><strong>4.4\u79cd\u663e\u793a\u7684\u547d\u540d\u5f3a\u5236\u7c7b\u578b\u8f6c\u6362<\/strong><\/p>\n\n\n\n<p>    1.tatic_cast\u2f64\u4e8e\u4e24\u4e2a\u7c7b\u578b\u610f\u4e49\u76f8\u8fd1\u7684\u8f6c\u6362\uff0c\u8fd9\u4e2a\u8f6c\u6362\u662f\u5177\u6709\u660e\u786e\u5b9a\u4e49\u7684\uff0c\u53ea\u8981\u5e95\u5c42\u4e0d\u5305\u542bconst\uff0c\u90fd\u53ef\u4ee5\u4f7f\u2f64static_cast\u3002 <\/p>\n\n\n\n<p>    2.reinterpret_cast\u2f64\u4e8e\u4e24\u4e2a\u7c7b\u578b\u610f\u4e49\u4e0d\u76f8\u8fd1\u7684\u8f6c\u6362\uff0creinterpret\u662f\u91cd\u65b0\u89e3\u91ca\u7684\u610f\u601d\uff0c\u901a\u5e38\u4e3a\u8fd0\u7b97\u5bf9\u8c61\u7684\u4f4d\u6a21\u5f0f\u63d0\u4f9b\u8f83\u4f4e\u5c42\u6b21\u4e0a\u7684\u91cd\u65b0\u89e3\u91ca\uff0c\u4e5f\u5c31\u662f\u8bf4\u8f6c\u6362\u540e\u5bf9\u539f\u6709\u5185\u5b58\u7684\u8bbf\u95ee\u89e3\u91ca\u5df2\u7ecf\u5b8c\u5168\u6539\u53d8\u4e86\uff0c\u2fae\u5e38\u7684\u2f24\u80c6\u3002\u6240\u4ee5\u6211\u4eec\u8981\u8c28\u614e\u4f7f\u2f64\uff0c\u6e05\u695a\u77e5\u9053\u8fd9\u6837\u8f6c\u6362\u662f\u6ca1\u6709\u5185\u5b58\u8bbf\u95ee\u5b89\u5168\u95ee\u9898\u7684\u3002<\/p>\n\n\n\n<p>    3. const_cast\u2f64\u4e8econst\u7c7b\u578b\u5230\u2faeconst\u7c7b\u578b\u7684\u8f6c\u6362\uff0c\u53bb\u6389\u4e86const\u5c5e\u6027\uff0c\u4e5f\u662f\u2f00\u6837\u7684\u6211\u4eec\u8981\u8c28\u614e\u4f7f\u2f64\uff0c\u5426\u5219\u53ef\u80fd\u4f1a\u51fa\u73b0\u610f\u60f3\u4e0d\u5230\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<p>    4.dynamic_cast\u2f64\u4e8e\u5c06\u57fa\u7c7b\u7684\u6307\u9488\u6216\u8005\u5f15\u2f64\u5b89\u5168\u7684\u8f6c\u6362\u6210\u6d3e\u2f63\u7c7b\u7684\u6307\u9488\u6216\u8005\u5f15\u2f64\u3002\u5982\u679c\u57fa\u7c7b\u7684\u6307\u9488\u6216\u8005\u5f15\u2f64\u65f6\u6307\u5411\u6d3e\u2f63\u7c7b\u5bf9\u8c61\u7684\uff0c\u5219\u8f6c\u6362\u56de\u6d3e\u2f63\u7c7b\u6307\u9488\u6216\u8005\u5f15\u2f64\u65f6\u53ef\u4ee5\u6210\u529f\u7684\uff0c\u5982\u679c\u57fa\u7c7b\u7684\u6307\u9488\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\uff0c\u5219\u8f6c\u6362\u5931\u8d25\u8fd4\u56denullptr\uff0c\u5982\u679c\u57fa\u7c7b\u5f15\u2f64\u6307\u5411\u57fa\u7c7b\u5bf9\u8c61\uff0c\u5219\u8f6c\u6362\u5931\u8d25\uff0c\u629b\u51fabad_cast\u5f02\u5e38\u3002 \u5176\u6b21dynamic_cast\u8981\u6c42\u57fa\u7c7b\u5fc5\u987b\u662f\u591a\u6001\u7c7b\u578b\uff0c\u4e5f\u5c31\u662f\u57fa\u7c7b\u4e2d\u5fc5\u987b\u6709\u865a\u51fd\u6570\u3002\u56e0\u4e3adynamic_cast\u662f\u8fd0\u2f8f\u65f6\u901a\u8fc7\u865a\u8868\u4e2d\u5b58\u50a8\u7684type_info\u5224\u65ad\u57fa\u7c7b\u6307\u9488\u6307\u5411\u7684\u662f\u57fa\u7c7b\u5bf9\u8c61\u8fd8\u662f\u6d3e\u2f63\u7c7b\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u4eca\u5929\u7684\u66f4\u65b0\u5c31\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u6709\u9519\u8bef\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff01\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>gitee\u4ed3\u5e93\uff1ahttps:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/c&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[4],"tags":[],"class_list":["post-240","post","type-post","status-publish","format-standard","hentry","category-c"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=240"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions"}],"predecessor-version":[{"id":855,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/240\/revisions\/855"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}