{"id":1133,"date":"2024-08-17T22:12:48","date_gmt":"2024-08-17T14:12:48","guid":{"rendered":"http:\/\/blog.chxzking.cn\/?p=1133"},"modified":"2024-08-25T21:32:34","modified_gmt":"2024-08-25T13:32:34","slug":"c%e7%9a%84%e7%b1%bb","status":"publish","type":"post","link":"http:\/\/blog.chxzking.cn\/index.php\/2024\/08\/17\/c%e7%9a%84%e7%b1%bb\/","title":{"rendered":"C++\u7684\u7c7b"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\"><kbd>\u524d\u8a00<\/kbd><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5b66\u4e60C\u548cC++\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8001\u5e08\u901a\u5e38\u4f1a\u7b3c\u7edf\u5730\u8bf4C++\u5305\u542b\u4e86C\uff0c\u4e5f\u5c31\u662f\u90a3\u53e5\u719f\u6089\u7684\u8bdd\u2014\u2014C++\u662fC\u7684\u8d85\u96c6\u3002\u8fd9\u79cd\u8bf4\u6cd5\u867d\u7136\u5728\u67d0\u79cd\u7a0b\u5ea6\u4e0a\u662f\u6b63\u786e\u7684\uff0c\u4f46\u5e76\u4e0d\u5b8c\u5168\u51c6\u786e\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u53ef\u5426\u8ba4\uff0cC++\u786e\u5b9e\u5305\u542b\u4e86C\u7684\u8bb8\u591a\u7279\u6027\uff0c\u4f8b\u5982\u6307\u9488\u3001\u7ed3\u6784\u4f53\u548c\u9884\u5904\u7406\u5668\u6307\u4ee4\u7b49\u3002\u7136\u800c\uff0c\u5c06\u4e24\u79cd\u8bed\u8a00\u5408\u5e76\u6210\u4e00\u79cd\u663e\u7136\u662f\u4e0d\u5408\u7406\u7684\u3002C\u548cC++\u867d\u7136\u5171\u4eab\u8bb8\u591a\u8bed\u6cd5\u548c\u6982\u5ff5\uff0c\u4f46\u5b83\u4eec\u7684\u8bbe\u8ba1\u7406\u5ff5\u548c\u5e94\u7528\u573a\u666f\u6709\u7740\u663e\u8457\u7684\u533a\u522b\u3002\u6216\u8005\u8bf4\uff0c\u5728\u6211\u770b\u6765\uff0cC\u548cC++\u4e0d\u8fc7\u662f\u5173\u952e\u5b57\u76f8\u4f3c\u7684\u4e24\u79cd\u8bed\u8a00\uff0c\u5c24\u5176\u662f\u8d8a\u6df1\u5165\u5b66\u4e60\uff0c\u8d8a\u80fd\u7406\u89e3\u4e24\u79cd\u8bed\u8a00\u7684\u8bbe\u8ba1\u7406\u5ff5\u7684\u4e0d\u540c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728C\u7684\u4e16\u754c\u91cc\uff0c\u5b83\u662f\u4e00\u79cd\u9ad8\u5ea6\u7b80\u7ea6\u7684\u9762\u5411\u8fc7\u7a0b\u7684\u8bed\u8a00\u3002C\u8bed\u8a00\u4ee5\u5176\u7cbe\u5de7\u7684\u8bbe\u8ba1\u548c\u9ad8\u6548\u7684\u6267\u884c\u8457\u79f0\u3002\u4f9d\u9760\u6307\u9488\uff0c\u4f60\u53ef\u4ee5\u81ea\u7531\u64cd\u63a7\u786c\u4ef6\uff0c\u76f4\u63a5\u8bbf\u95ee\u5185\u5b58\u5730\u5740\uff0c\u8fd9\u4f7f\u5f97C\u5728\u7cfb\u7edf\u7f16\u7a0b\u548c\u5d4c\u5165\u5f0f\u5f00\u53d1\u4e2d\u5360\u636e\u91cd\u8981\u5730\u4f4d\u3002C\u8bed\u8a00\u7684\u7f16\u8bd1\u5668\u901a\u5e38\u751f\u6210\u9ad8\u5ea6\u4f18\u5316\u7684\u673a\u5668\u7801\uff0c\u786e\u4fdd\u7a0b\u5e8f\u7684\u8fd0\u884c\u6548\u7387\u6781\u9ad8\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u5728C++\u7684\u4e16\u754c\u91cc\uff0c\u5b83\u662f\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u8bed\u8a00\u3002C++\u5728C\u7684\u57fa\u7840\u4e0a\u589e\u52a0\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7279\u6027\uff0c\u5982\u7c7b\u3001\u7ee7\u627f\u3001\u591a\u6001\u3001\u6a21\u677f\u548c\u5f02\u5e38\u5904\u7406\u7b49\u3002\u667a\u80fd\u6307\u9488\u548c\u5f15\u7528\u7b49\u7279\u6027\u90fd\u5728\u9690\u7ea6\u5212\u5206\u7740\u5b83\u4e0eC\u7684\u754c\u9650\u3002\u76f8\u6bd4C\uff0cC++\u591a\u4e86\u8bb8\u591a\u9650\u5236\uff0c\u4e0d\u65ad\u5f15\u5165\u7684\u65b0\u8bed\u6cd5\u4f7f\u5f97\u5b83\u80cc\u4e0a\u6c89\u91cd\u7684\u5305\u88b1\u3002\u7c7b\u662fC++\u4e0eC\u6700\u5927\u7684\u533a\u522b\u4e4b\u4e00\uff0c\u8bb8\u591a\u65b0\u7279\u6027\u90fd\u662f\u4e3a\u4e86\u7c7b\u7684\u53d1\u5c55\u3002\u7531\u4e8e\u8fdb\u4e00\u6b65\u7684\u62bd\u8c61\uff0cC++\u7684\u8fd0\u884c\u6548\u7387\u76f8\u6bd4C\u6765\u8bf4\u8981\u4f4e\u5f88\u591a\uff0c\u4f46\u5f00\u53d1\u6548\u7387\u5374\u63d0\u9ad8\u4e86\u4e0d\u5c11\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f8\u8f83\u4e8e\u5176\u4ed6\u8bed\u8a00C++\u51ed\u501f\u5176\u9ad8\u8fd0\u884c\u6548\u7387\u548c\u5386\u53f2\u53d1\u5c55\uff0c\u6210\u5c31\u4e86\u5e9e\u5927\u7684\u751f\u6001\u7cfb\u7edf\u3002\u76f4\u5230\u4eca\u5929\uff0cC++\u4f9d\u9760\u7740\u7c7b\u548c\u6a21\u677f\uff0c\u5728\u5404\u5927\u7cfb\u7edf\u7ea7\u7f16\u7a0b\u4e2d\u7ecf\u4e45\u4e0d\u8870\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><kbd>\u5c01\u88c5<\/kbd><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u7a76\u7adf\u662f\u4ec0\u4e48\uff1f\u7c7b\u5c31\u662f\u4e00\u4e2a\u9ed8\u8ba4\u6743\u9650\u4e3a\u79c1\u6709\u7684\u7ed3\u6784\u4f53\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u7ed3\u6784\u4f53\u662fc\u7684\u7ed3\u6784\u4f53\u7684\u5347\u7ea7\u7248\u672c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728c\u4e2d\u7684\u7ed3\u6784\u4f53\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ed3\u6784\u4f53\u5728\u4e0d\u5b9e\u4f8b\u5316\u65f6\uff0c\u4e0d\u5360\u7528\u5185\u5b58\uff0c\u5b83\u662f\u4e00\u79cd\u6570\u636e\u7ec4\u5408\u7684\u65b9\u5f0f<\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ea\u80fd\u4e3a\u53d8\u91cf<\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\u6210\u5458\u5728\u5b9a\u4e49\u65f6\u4e0d\u80fd\u88ab\u521d\u59cb\u5316<\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\u7684\u8bbf\u95ee\u6743\u9650\u662f\u516c\u5f00\u7684<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u5728c++\u4e2d\uff0c\u7ed3\u6784\u4f53\u5177\u6709\u4e86\u8fdb\u4e00\u6b65\u6269\u5c55\uff0c\u6bd4\u5982\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7ed3\u6784\u4f53\u7684\u6210\u5458\u53ef\u4ee5\u4e3a\u51fd\u6570<\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\u6210\u5458\u5141\u8bb8\u5728\u88ab\u5b9a\u4e49\u65f6\u521d\u59cb\u5316<code>\uff08C++11\u53ca\u4ee5\u540e\uff09\u3002<\/code><\/li>\n\n\n\n<li>\u7ed3\u6784\u4f53\u8bbf\u95ee\u6743\u9650\u4e3a\u516c\u5f00\uff0c\u4f46\u662f\u53ef\u4ee5\u8bbe\u7f6e\u4e0d\u540c\u6743\u9650<\/li>\n\n\n\n<li>&#8230;&#8230;<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3a\u4e86\u533a\u522b\u4e0ec\u4e2d\u7684\u7ed3\u6784\u4f53\uff0cc++\u7ed9\u5177\u6709\u9ed8\u8ba4\u79c1\u6709\u6743\u9650\u7684\u8d85\u7ea7\u7ed3\u6784\u4f53\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e00\u4e2a\u65b0\u7684\u5173\u952e\u5b57\u2014\u2014class(\u7c7b)\uff0c\u4e0d\u8fc7\u66fe\u7ecf\u7684struct\u5173\u952e\u5b57\u4f9d\u65e7\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u8fd8\u6709\u4e00\u4e2a\u53ef\u4ee5\u8865\u5145\u7684\u70b9\uff1a\u4ece\u672c\u8d28\u4e0a\u8bb2\uff0c\u7c7b\u53ea\u4e0d\u8fc7\u662f\u5f00\u53d1\u8005\u5c42\u6b21\u4e0a\u7684\u62bd\u8c61\uff0c\u5176\u5b9e\u5728\u5e95\u5c42\u4f9d\u65e7\u662f\u4e00\u4e2a\u7ed3\u6784\u4f53\u6570\u636e\u6808\uff0c\u81f3\u4e8e\u6240\u8c13\u7684\u6210\u5458\u51fd\u6570\uff0c\u5176\u5b9e\u662f\u4e00\u4e2a\u5305\u542bthis\u6307\u9488\u7684\u666e\u901a\u5168\u5c40\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">c++\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass student{\npublic:\n    void func(int num);\nprivate:\n    int num = 1314520;\n};\n\nvoid student::func(int num){\n    this-&gt;num = num;\n}\n\nint main(){\n    student a;\n    a.func(10);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6c47\u7f16\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\t.file\t\"main.cpp\"\n\t.text\n\t.local\t_ZStL8__ioinit\n\t.comm\t_ZStL8__ioinit,1,1\n\t.align 2\n\t.globl\t_ZN7student4funcEi\n\t.type\t_ZN7student4funcEi, @function\n;\u6210\u5458\u51fd\u6570 void func()\u5b9a\u4e49\n_ZN7student4funcEi:\n.LFB1731:\n\t.cfi_startproc\n\tendbr64\n\t;\u51fd\u6570\u6808\u9884\u5904\u7406\n\tpushq\t%rbp\n\t.cfi_def_cfa_offset 16\n\t.cfi_offset 6, -16\n\tmovq\t%rsp, %rbp\n\t.cfi_def_cfa_register 6\n\t;main\u5c06num\u7684\u5730\u5740\u548c\u4f20\u5165\u7684\u53c2\u657010\u5206\u522b\u653e\u8fdb\u4e86 rdi\u5bc4\u5b58\u5668\u548cesi\u5bc4\u5b58\u5668\uff0c\u5206\u522b\u5b58\u50a8\u5728func\u51fd\u6570\u6808\u4e0a\n\tmovq\t%rdi, -8(%rbp)\n\tmovl\t%esi, -12(%rbp)\n\t;\u8bfb\u53d6\u4e4b\u524d\u5b58\u5165\u7684\u53d8\u91cf\uff08\u5176\u5b9e\u8fd9\u4e2a\u6709\u70b9\u591a\u4f59\uff0c\u7b97\u662f\u4f18\u5316\u4e0d\u5230\u4f4d\uff09\n\tmovq\t-8(%rbp), %rax\n\tmovl\t-12(%rbp), %edx\n\t;\u5c06\u53c2\u657010\u653e\u8fdbnum\u7684\u5730\u5740\u5bf9\u5e94\u7684\u4f4d\u7f6e\u4e2d\n\tmovl\t%edx, (%rax)\n        ;\u4e4b\u524d\u4e94\u53e5\u4ee3\u7801\u5176\u5b9e\u5b8c\u5168\u53ef\u4ee5\u7f29\u51cf\u6210movl\t%esi, (%rdi)\u4e00\u53e5\u4ee3\u7801\uff0c\u4e0d\u8fc7\u8fd9\u91cc\u5e76\u4e0d\u662f\u672c\u6b21\u8ba8\u8bba\u7684\u91cd\u5fc3\uff0c\u53ea\u662f\u4e00\u4e2a\u5c0f\u5c0f\u7684\u5410\u69fd\n\tnop\n\tpopq\t%rbp\n\t.cfi_def_cfa 7, 8\n\tret\n\t.cfi_endproc\n.LFE1731:\n\t.size\t_ZN7student4funcEi, .-_ZN7student4funcEi\n\t.globl\tmain\n\t.type\tmain, @function\n;main\u51fd\u6570\u7684\u5b9e\u73b0\nmain:\n.LFB1732:\n\t.cfi_startproc\n\tendbr64\n\t;\u51fd\u6570\u6808\u9884\u5904\u7406\n\tpushq\t%rbp\n\t.cfi_def_cfa_offset 16\n\t.cfi_offset 6, -16\n\tmovq\t%rsp, %rbp\n\t.cfi_def_cfa_register 6\n\t;\u5206\u914d\u6808\u7a7a\u95f416\u4e2a\u5b57\u8282\n\tsubq\t$16, %rsp\n\n\tmovq\t%fs:40, %rax\n\tmovq\t%rax, -8(%rbp)\n\t;eax\u5bc4\u5b58\u5668\u6e05\u96f6\n\txorl\t%eax, %eax\n\t;\u79c1\u6709\u53d8\u91cfnum\u521d\u59cb\u5316\uff0c\u4e8b\u5b9e\u4e0a\u5728\u6c47\u7f16\u4e2d\u662f\u4e0d\u5b58\u5728\u53d8\u91cf\u7684\u6982\u5ff5\u7684\uff0c\u4e00\u5207\u90fd\u53d8\u91cf\u4e0d\u8fc7\u662f\u5bf9\u6808\u57fa\u6307\u9488\u504f\u79fb\u91cf\u7684\u89e3\u6790\n\tmovl\t$1314520, -12(%rbp)\n\t;\u5c06num\u7684\u5730\u5740\u52a0\u8f7d\u5230rax\u5bc4\u5b58\u5668\u4e2d\n\tleaq\t-12(%rbp), %rax\n\t;func()\u51fd\u6570\u7684\u53c2\u6570\u4f20\u9012\u4e3a10\n\tmovl\t$10, %esi\n\t;num\u7684\u5730\u5740\u4f5c\u4e3a\u7c7b\u7684this\u6307\u9488\u6307\u5411\u7684\u7a7a\u95f4\uff0c\u540c\u65f6\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u5230func\u51fd\u6570\u4e2d\n\tmovq\t%rax, %rdi\n\t;\u8c03\u7528\u6210\u5458\u51fd\u6570\uff0c\u5176\u5b9e\u5c31\u662f\u8c03\u7528\u4e00\u4e2a\u5168\u5c40\u51fd\u6570\n\tcall\t_ZN7student4funcEi\n\t \n\tmovl\t$0, %eax\n\tmovq\t-8(%rbp), %rdx\n\tsubq\t%fs:40, %rdx\n\tje\t.L4\n\tcall\t__stack_chk_fail@PLT\n.L4:\n\tleave\n\t.cfi_def_cfa 7, 8\n\tret\n\t.cfi_endproc\n.LFE1732:\n\t.size\tmain, .-main\n\t.type\t_Z41__static_initialization_and_destruction_0ii, @function\n_Z41__static_initialization_and_destruction_0ii:\n.LFB2230:\n\t.cfi_startproc\n\tendbr64\n\tpushq\t%rbp\n\t.cfi_def_cfa_offset 16\n\t.cfi_offset 6, -16\n\tmovq\t%rsp, %rbp\n\t.cfi_def_cfa_register 6\n\tsubq\t$16, %rsp\n\tmovl\t%edi, -4(%rbp)\n\tmovl\t%esi, -8(%rbp)\n\tcmpl\t$1, -4(%rbp)\n\tjne\t.L7\n\tcmpl\t$65535, -8(%rbp)\n\tjne\t.L7\n\tleaq\t_ZStL8__ioinit(%rip), %rax\n\tmovq\t%rax, %rdi\n\tcall\t_ZNSt8ios_base4InitC1Ev@PLT\n\tleaq\t__dso_handle(%rip), %rax\n\tmovq\t%rax, %rdx\n\tleaq\t_ZStL8__ioinit(%rip), %rax\n\tmovq\t%rax, %rsi\n\tmovq\t_ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax\n\tmovq\t%rax, %rdi\n\tcall\t__cxa_atexit@PLT\n.L7:\n\tnop\n\tleave\n\t.cfi_def_cfa 7, 8\n\tret\n\t.cfi_endproc\n.LFE2230:\n\t.size\t_Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii\n\t.type\t_GLOBAL__sub_I__ZN7student4funcEi, @function\n_GLOBAL__sub_I__ZN7student4funcEi:\n.LFB2231:\n\t.cfi_startproc\n\tendbr64\n\tpushq\t%rbp\n\t.cfi_def_cfa_offset 16\n\t.cfi_offset 6, -16\n\tmovq\t%rsp, %rbp\n\t.cfi_def_cfa_register 6\n\tmovl\t$65535, %esi\n\tmovl\t$1, %edi\n\tcall\t_Z41__static_initialization_and_destruction_0ii\n\tpopq\t%rbp\n\t.cfi_def_cfa 7, 8\n\tret\n\t.cfi_endproc\n.LFE2231:\n\t.size\t_GLOBAL__sub_I__ZN7student4funcEi, .-_GLOBAL__sub_I__ZN7student4funcEi\n\t.section\t.init_array,\"aw\"\n\t.align 8\n\t.quad\t_GLOBAL__sub_I__ZN7student4funcEi\n\t.hidden\t__dso_handle\n\t.ident\t\"GCC: (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0\"\n\t.section\t.note.GNU-stack,\"\",@progbits\n\t.section\t.note.gnu.property,\"a\"\n\t.align 8\n\t.long\t1f - 0f\n\t.long\t4f - 1f\n\t.long\t5\n0:\n\t.string\t\"GNU\"\n1:\n\t.align 8\n\t.long\t0xc0000002\n\t.long\t3f - 2f\n2:\n\t.long\t0x3\n3:\n\t.align 8\n4:\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd8\u6709\u53e6\u5916\u4e00\u79cd\u65b9\u5f0f\u53ef\u4ee5\u6bd4\u8f83\u76f4\u89c2\u7684\u89c2\u5bdf\u5230\u7c7b\u7684\u6570\u636e\u548c\u51fd\u6570\u5206\u79bb\u7684\u60c5\u51b5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class my_class\n{\npublic:\n    void func();\n};\nvoid my_class::func(){\n    cout&lt;&lt;\"hello world\"&lt;&lt;endl;\n}\nint main(){\n   my_class* p = nullptr;\n   p-&gt;func();\n}   <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>hello world<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u5728\u6ca1\u6709\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u60c5\u51b5\u4e0b\uff0c\u80fd\u8c03\u7528\u6210\u5458\u51fd\u6570\u3002\u4e0d\u8fc7\u8fd9\u4e2a\u4ee3\u7801\u5b9e\u9645\u4e0a\u662f\u5229\u7528\u4e86\u4e00\u4e2a\u7f16\u8bd1\u5668\u7684bug\uff0c\u56e0\u4e3ag++\u7f16\u8bd1\u5668\u662f\u4e0d\u68c0\u67e5\u6307\u9488\u6240\u6307\u5411\u7684\u5730\u5740\u7684\u6b63\u786e\u6027\u7684\uff0c\u8fd9\u91cc\u4f7f\u7528\u4e00\u4e2a\u7a7a\u6307\u9488\u9003\u8fc7\u4e86\u7f16\u8bd1\u7684\u8bed\u6cd5\u68c0\u67e5\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7c7b\u7684\u6743\u9650<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u7c7b\u5177\u6709\u4e09\u79cd\u4e0d\u540c\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><tbody><tr><td>\u79c1\u6709<code> private <\/code><\/td><td>\u79c1\u6709\u6743\u9650\u7684\u4f5c\u7528\u662f\u5c06\u7c7b\u4e2d\u7684\u6210\u5458\u8f6c\u4e3a\u4ec5\u7c7b\u81ea\u8eab\u53ef\u4ee5\u8bbf\u95ee\uff0c\u5b83\u662f\u7c7b\u7684\u9ed8\u8ba4\u8bbf\u95ee\u6743\u9650\u3002<\/td><\/tr><tr><td>\u516c\u6709 <code>public <\/code><\/td><td>\u516c\u6709\u5c5e\u6027\u5141\u8bb8\u6240\u6709\u7684\u5916\u90e8\u64cd\u4f5c\u5bf9\u5176\u8fdb\u884c\u8bbf\u95ee<\/td><\/tr><tr><td>\u4fdd\u62a4<code> protected <\/code><\/td><td>\u4fdd\u62a4\u6743\u9650\u901a\u5e38\u4e0e\u79c1\u6709\u6743\u9650\u7c7b\u4f3c\uff0c\u5b83\u80fd\u963b\u6b62\u5916\u90e8\u7684\u8bbf\u95ee\uff0c\u4e0d\u8fc7\u5b83\u901a\u5e38\u88ab\u4f7f\u7528\u5728\u7ee7\u627f\u65b9\u9762\uff0c\u56e0\u4e3a\u79c1\u6709\u5c5e\u6027\u662f\u4e0d\u80fd\u88ab\u7ee7\u627f\u7684\uff0c\u800c\u4fdd\u62a4\u5141\u8bb8\u88ab\u7ee7\u627f\u3002<\/td><\/tr><\/tbody><\/table><figcaption class=\"wp-element-caption\">\u7c7b\u7684\u6743\u9650<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class my_class\n{\nprivate:\n    int a;\/\/\u79c1\u6709\u6210\u5458\npublic:\n    int b;\/\/\u516c\u6709\u6210\u5458\nprotected:\n    int c;\/\/\u4fdd\u62a4\u6210\u5458\n};<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u6210\u5458\u51fd\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">C++\u7684\u6210\u5458\u51fd\u6570\u662f\u5b9a\u4e49\u5728\u7c7b\u5185\u90e8\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u64cd\u4f5c\u7c7b\u7684\u5bf9\u8c61\u548c\u8bbf\u95ee\u7c7b\u7684\u6210\u5458\u53d8\u91cf\u3002\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u548c\u4fdd\u62a4\u6210\u5458\u3002\u6b64\u5916\uff0c\u6210\u5458\u51fd\u6570\u81ea\u8eab\u4e5f\u53ef\u4ee5\u8bbe\u7f6e\u6743\u9650\uff0c\u5b83\u7684\u6743\u9650\u7279\u6027\u4e0e\u6210\u5458\u53d8\u91cf\u7684\u6743\u9650\u7279\u6027\u4e00\u81f4\u3002\u5373\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>public<\/code>\u6210\u5458\u51fd\u6570\u53ef\u4ee5\u88ab\u7c7b\u7684\u5916\u90e8\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><code>protected<\/code>\u6210\u5458\u51fd\u6570\u53ea\u80fd\u88ab\u7c7b\u548c\u5176\u6d3e\u751f\u7c7b\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li><code>private<\/code>\u6210\u5458\u51fd\u6570\u53ea\u80fd\u88ab\u7c7b\u5185\u90e8\u8bbf\u95ee\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u53ef\u4ee5\u5728\u7c7b\u7684\u5185\u90e8\u5b9a\u4e49\u6216\u8005\u7c7b\u7684\u5916\u90e8\u5b9a\u4e49\uff0c<mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u51e0\u4e4e<\/mark>\u6240\u6709\u7684\u6210\u5458\u51fd\u6570\u90fd\u6709<code>this<\/code>\u6307\u9488\u3002\u4e0d\u8fc7\u901a\u5e38\u4e0d\u63a8\u8350\u51fd\u6570\u5b9a\u4e49\u5199\u5728\u7c7b\u7684\u5185\u90e8\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u901a\u5e38\u4f1a\u5185\u8054\u7c7b\u7684\u5185\u90e8\u7684\u51fd\u6570\uff0c\u5982\u679c\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u4e86\u8fd9\u4e2a\u7c7b\u7684\u6210\u5458\uff0c\u90a3\u4e48\u5728\u7f16\u8bd1\u9636\u6bb5\uff0c\u4f1a\u5728\u5c06\u6210\u5458\u51fd\u6570\u7684\u4ee3\u7801\u590d\u5236\u5230\u8be5\u51fd\u6570\u7684\u5185\u90e8\u4e00\u8d77\u7f16\u8bd1\uff0c\u8ba9\u51fd\u6570\u53d8\u5f97\u81c3\u80bf\u3002\u800c\u4e14\u7531\u4e8e\u5b9a\u4e49\u58f0\u660e\u653e\u5728\u4e00\u8d77\uff0c\u6574\u4e2a\u7c7b\u4f1a\u53d8\u5f97\u53ca\u5176\u6df7\u4e71\uff0c\u4e0d\u5229\u4e8e\u5f00\u53d1\u8005\u7406\u89e3\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u8c03\u7528\u65b9\u5f0f\u4e5f\u5f88\u7b80\u5355\uff0c\u901a\u8fc7\u5bf9\u8c61\u7684<code> . <\/code>\u7b26\u53f7\u8c03\u7528\uff0c\u6bd4\u5982 <code>a.func()<\/code><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u666e\u901a\u6210\u5458\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u6700\u666e\u904d\u7684\u4e00\u79cd\u6210\u5458\u51fd\u6570\uff0c\u5b83\u643a\u5e26this\u6307\u9488\uff0c\u53ef\u4ee5\u81ea\u7531\u8bbf\u95ee\u7c7b\u7684\u6210\u5458\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class my_class\n{\nprivate:\n    int a;\/\/\u79c1\u6709\u6210\u5458\npublic:\n    void set(int a);\/\/\u58f0\u660e\n};\n\/\/\u5b9a\u4e49\nvoid my_class::set(int a){\n    this-&gt;a = a;\n}\n\u6216\u8005\nclass my_class\n{\nprivate:\n    int a;\/\/\u79c1\u6709\u6210\u5458\npublic:\n    void my_class::set(int a){\n        this-&gt;a = a;\n    }\n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728c\u8bed\u8a00\u4e2d\uff0c\u6211\u4eec\u5e38\u5e38\u8bbe\u8ba1\u4e86\u4e00\u4e2a<code>Init()<\/code>\u51fd\u6570\u5bf9\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4f46\u662f\u5728c++\u4e2d\u63d0\u51fa\u6765\u6784\u9020\u51fd\u6570\u7684\u6982\u5ff5\uff0c\u53ef\u4ee5\u53bb\u9664\u663e\u5f0f\u8c03\u7528\u521d\u59cb\u5316\u51fd\u6570\u7684\u7e41\u7410\u6d41\u7a0b\uff0c\u800c\u4e14\u4e5f\u53ef\u4ee5\u675c\u7edd\u5f00\u53d1\u8005\u5fd8\u8bb0\u8fdb\u884c\u521d\u59cb\u5316\u6216\u8005\u91cd\u590d\u521d\u59cb\u5316\u9020\u6210\u7684\u672a\u5b9a\u4e49\u884c\u4e3a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6784\u9020\u51fd\u6570\u6700\u5927\u7684\u7279\u70b9\u662f\u5b83\u7684\u51fd\u6570\u540d\u4e0e\u7c7b\u76f8\u540c\uff0c\u5e76\u4e14\u6ca1\u6709\u8fd4\u56de\u503c\u3002\u6784\u9020\u51fd\u6570\u5206\u4e3a\u9ed8\u8ba4\u6784\u9020\u548c\u6709\u53c2\u6784\u9020\u4e24\u79cd\u7c7b\u578b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4e00\u4e2a\u7c7b\u4e2d\u6ca1\u6709\u5b9a\u4e49\u4efb\u4f55\u6784\u9020\u51fd\u6570\uff08\u5305\u62ec\u6709\u53c2\u6784\u9020\uff09\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u5c31\u4f1a\u9009\u62e9\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\uff0c\u5982\u679c\u5b9a\u4e86\u6709\u53c2\u6784\u9020\uff0c\u7f16\u8bd1\u5219\u4e0d\u662f\u518d\u751f\u6210\u9ed8\u8ba4\u6784\u9020\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u9ed8\u8ba4\u6784\u9020<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class my_class{\nprivate:\n    int a;\/\/\u79c1\u6709\u6210\u5458\npublic:\n    my_class();\/\/\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\n};\nmy_class::my_class(){\n    cout&lt;&lt;\"\u6211\u662f\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\"&lt;&lt;endl;\n}\nint main(){\n    my_class a;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u5b9a\u4e49\u4e86\u4e00\u4e2a\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5f53\u7c7b\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\uff0c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u4f1a\u88ab\u8c03\u7528\u3002\u5982\u679c\u6211\u4eec\u5c06\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7684\u5b9a\u4e49\u5b58\u653e\u5230\u7c7b\u7684\u79c1\u7528\u4e2d\uff0c\u90a3\u4e48\u8fd9\u4e2a\u7c7b\u5c31\u65e0\u6cd5\u88ab\u5b9e\u4f8b\u5316\uff0c\u5e76\u4e14\u4ee3\u7801\u7f16\u8bd1\u4e5f\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u6709\u53c2\u6784\u9020<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u53c2\u6784\u9020\u5c31\u662f\u5e26\u6709\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class my_class{\nprivate:\n    int a;\/\/\u79c1\u6709\u6210\u5458\npublic:\n    my_class(int a);\n};\nmy_class::my_class(int a){\n    cout&lt;&lt;\"\u6211\u662f\u6709\u53c2\u6784\u9020\u51fd\u6570\"&lt;&lt;endl;\n}\nint main(){\n    my_class a(10);\n}   <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u80fd\u5728\u521d\u59cb\u5316\u7684\u65f6\u5019\u4f20\u5165\u53c2\u6570\uff0c\u6709\u53c2\u6784\u9020\u51fd\u6570\u662f\u5141\u8bb8\u91cd\u8f7d\u7684\uff0c\u53ef\u4ee5\u81ea\u5df1\u8bbe\u7f6e\u4e0d\u901a\u8fc7\u53c2\u6570\u7c7b\u578b\u7684\u6784\u9020\u51fd\u6570\u3002\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f\uff0c\u6709\u53c2\u6784\u9020\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u5668\u4e0d\u4f1a\u6dfb\u52a0\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\uff0c\u5982\u679c\u9700\u8981\u9ed8\u8ba4\u6784\u9020\uff0c\u90a3\u4e48\u5fc5\u987b\u8981\u663e\u5f0f\u5b9a\u4e49\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u8c03\u7528\u6784\u9020\u51fd\u6570\u7684\u65b9\u5f0f<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fdb\u884c\u7c7b\u7684\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\uff0c\u5176\u5b9e\u5c31\u5728\u540c\u65f6\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u8fd9\u53ea\u662f\u8c03\u7528\u7684\u4e00\u79cd\u65b9\u5f0f\u88ab\u79f0\u76f4\u63a5\u521d\u59cb\u5316\u3002\u800c\u521d\u59cb\u5316\u7684\u65b9\u6cd5\uff0c\u5927\u81f4\u662f\u5206\u4e3a\u4e24\u79cd\uff0c\u53e6\u5916\u4e00\u79cd\u5219\u662f\u62f7\u8d1d\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u76f4\u63a5\u521d\u59cb\u5316<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5\u521d\u59cb\u5316\u5f80\u5f80\u5b9e\u5728\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u5bf9\u521d\u59cb\u5316\u683c\u5f0f\u5339\u914d\u7c7b\u4e2d\u7684\u6784\u9020\u51fd\u6570\u7684\u5f62\u5f0f\uff0c\u7136\u540e\u8c03\u7528\u8be5\u51fd\u6570\u5bf9\u7c7b\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u62f7\u8d1d\u521d\u59cb\u5316<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u62f7\u8d1d\u521d\u59cb\u5316\u5219\u662f\u4f7f\u7528\u4e86\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5bf9\u5bf9\u8c61\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u521d\u59cb\u5316\u5b58\u5728\u4e00\u4e2a\u7f3a\u9677\uff0c\u90a3\u5c31\u662f\u7531\u7f16\u8bd1\u5668\u751f\u6210\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u662f\u57fa\u4e8e\u4e8c\u8fdb\u5236\u590d\u5236\uff0c\u4e5f\u5c31\u662f\u8bf4\u4f1a\u5c06\u88ab\u62f7\u8d1d\u5230\u53c2\u6570\u6240\u6709\u7684\u4fe1\u606f\u590d\u5236\u5230\u76ee\u6807\u5bf9\u8c61\u4e2d\uff0c\u5728\u591a\u6570\u60c5\u51b5\u4e0b\u662f\u53ef\u884c\u7684\uff0c\u4f46\u662f\u5728\u51fa\u73b0\u50cf\u52a8\u6001\u5185\u5b58\u7684\u60c5\u51b5\u4e0b\u5219\u4f1a\u5bfc\u81f4\u4e24\u4e2a\u5b9e\u4f8b\u4e4b\u95f4\u51fa\u73b0\u6570\u636e\u51b2\u7a81\uff0c\u6240\u4ee5\u9700\u8981\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a\u6df1\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\uff08\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u8be6\u89e3&#8211;&gt;<a href=\"#\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u89e3\u6790\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark><\/a>\uff09<\/p>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>\u5173\u4e8e\u6211\u5728\u603b\u7ed3\u62f7\u8d1d\u521d\u59cb\u5316\u7684\u4e00\u70b9\u63d2\u66f2<\/summary>\n<p class=\"wp-block-paragraph\">       \u56e0\u4e3a\u5728\u5b66\u4e60\u7684\u65f6\u5019\uff0c\u8fd9\u79cd\u57fa\u7840\u7684\u77e5\u8bc6\u5e76\u6ca1\u6709\u5f52\u6839\u7ed3\u5e95\u7684\u53bb\u5b66\u4e60\uff0c\u800c\u662f\u53ea\u77e5\u5176\u7136\u4e0d\u77e5\u5176\u6240\u4ee5\u7136\uff0c\u5f53\u65f6\u6211\u89c9\u5f97\u62f7\u8d1d\u521d\u59cb\u5316\u8fd9\u79cd\u7b56\u7565\u4e0d\u662f\u7406\u6240\u5f53\u7136\u7684\u5417\uff1f\u4f46\u662f\u5f53\u6211\u56de\u8fc7\u5934\u6765\u603b\u7ed3\u7684\u65f6\u5019\uff0c\u6211\u7a81\u7136\u610f\u8bc6\u5230\u4e86\u62f7\u8d1d\u521d\u59cb\u5316\u548c\u76f4\u63a5\u521d\u59cb\u5316\u4e4b\u95f4\u5b58\u5728\u5de8\u5927\u7684\u4e0d\u548c\u8c10\u611f\u3002<br>       \u6700\u9996\u5f53\u5176\u51b2\u7684\u4e00\u70b9\u5c31\u662f\u5f62\u5f0f\uff1a\u5728\u5b9a\u4e49\u4e2d\uff0c\u8001\u5e08\u603b\u662f\u8bf4\uff0c\u51fa\u73b0\u4e86\u7c7b\u7684\u5b9e\u4f8b\u5316\u5c31\u4f1a\u4e00\u5b9a\u4f1a\u8c03\u7528\u6784\u9020\u51fd\u6570\uff0c\u5f53\u7136\u8fd9\u4e2a\u662f\u4e0d\u53ef\u80fd\u540c\u65f6\u53d1\u751f\u7684\u3002\u5728\u5fae\u89c2\u5c3a\u5ea6\u4e0b\uff0c\u662f\u4e00\u4e2a\u7c7b\u88ab\u5b9e\u4f8b\u5316\u5b8c\u6210\u540e\uff0c\u4e5f\u5c31\u662f\u5206\u914d\u6808\u7a7a\u95f4\u540e\uff0c\u7f16\u8bd1\u5668\u4f1a\u8c03\u7528\u5bf9\u5e94\u7684\u6784\u9020\u51fd\u6570\u3002\u8fd9\u4e24\u8005\u662f\u7edd\u5bf9\u8fde\u7eed\u7684\uff0c\u4e2d\u95f4\u4e0d\u80fd\u6dfb\u52a0\u4efb\u4f55\u65e0\u5173\u7684\u64cd\u4f5c\uff0c\u8fd9\u662fc++\u6807\u51c6\u4e2d\u7684\u89c4\u5b9a\u3002<br>       \u4f46\u662f\u5f53\u62f7\u8d1d\u521d\u59cb\u5316\u6982\u5ff5\u51fa\u73b0\u540e\uff0c\u4e00\u5207\u90fd\u5947\u602a\u4e86\u8d77\u6765\uff0c\u56e0\u4e3a\u62f7\u8d1d\u521d\u59cb\u5316\u4f1a\u5728\u5b9e\u4f8b\u5316\u4e86\u4e00\u4e2a\u7c7b\u540e\uff0c\u5374\u8f6c\u5934\u53bb\u521b\u5efa\u4e86\u53e6\u5916\u4e00\u4e2a\u533f\u540d\u5bf9\u8c61\uff0c\u4e2d\u95f4\u5c45\u7136\u6ca1\u6709\u5339\u914d\u6784\u9020\u51fd\u6570\u3002\u4e24\u79cd\u521d\u59cb\u5316\u65b9\u5f0f\u6ca1\u6709\u4e00\u4e2a\u7edf\u4e00\u7684\u7406\u8bba\uff0c\u957f\u671f\u5199\u4ee3\u7801\u6216\u8005\u5fc3\u7ec6\u7684\u4eba\u5e94\u8be5\u80fd\u53d1\u73b0\u8fd9\u662f\u975e\u5e38\u522b\u626d\u7684\u4e00\u79cd\u60c5\u51b5\uff0c\u56e0\u4e3a\u5728\u6807\u51c6\u8bbe\u8ba1\u4e0a\uff0c\u7edf\u4e00\u5f80\u5f80\u662f\u4e00\u4e2a\u6807\u51c6\u6700\u7ec8\u7684\u5f62\u6001\uff0c\u5c31\u597d\u50cf\u7269\u7406\u5b66\u5bb6\u4e0d\u65ad\u8ffd\u6c42\u7edf\u4e00\u573a\u7406\u8bba\u4e00\u6837\uff0cc++\u6807\u51c6\u8bbe\u8ba1\u663e\u7136\u6ca1\u6709\u7edf\u4e00\u573a\u7406\u8bba\u90a3\u4e48\u8270\u96be\uff0c\u6240\u4ee5\u6211\u5728\u9a8c\u8bc1\u8fd9\u4e24\u79cd\u622a\u7136\u4e0d\u540c\u7684\u521d\u59cb\u5316\u65b9\u5f0f\u80cc\u540e\u5fc5\u7136\u5b58\u5728\u67d0\u79cd\u601d\u60f3\u4e0a\u7684\u7edf\u4e00\u3002<br>       \u4e3a\u6b64\uff0c\u6211\u67e5\u9605\u5927\u91cf\u7684\u8d44\u6599\uff0c\u5374\u5ffd\u7565\u4e86\u91cd\u8981\u7684\u4e00\u70b9\uff0c\u62f7\u8d1d\u521d\u59cb\u5316\u57fa\u4e8e\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u800c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662f\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\uff0c\u5728c++\u6807\u51c6\u4e2d\u63d0\u5230\u7684\u662f\u6784\u9020\u51fd\u6570\uff0c\u5e76\u6ca1\u6709\u7279\u6307\u54ea\u4e00\u7c7b\u3002\u90a3\u4e48\u4e00\u5207\u90fd\u89e3\u91ca\u901a\u4e86\uff0c\u5b9e\u9645\u4e0a\u62f7\u8d1d\u521d\u59cb\u5316\u786e\u5b9e\u662f\u5728\u5b9e\u4f8b\u5316\u540e\u4ec5\u4ec5\u8ddf\u7740\u6784\u9020\u51fd\u6570\uff0c\u4e0d\u8fc7\u8fd9\u4e2a\u6784\u9020\u51fd\u6570\u7684\u4e00\u4e2a\u53c2\u6570\u662f\u81ea\u8eab\uff0c\u53e6\u5916\u4e00\u4e2a\u53c2\u6570\u662f\u5f85\u62f7\u8d1d\u5230\u7c7b\u3002\u800c\u8fd9\u4e2a\u7b49\u53f7\uff0c\u6216\u8005\u8bf4\u6309\u7167\u4f2a\u4ee3\u7801\u7684\u5199\u6cd5\u5199\u6210\u8fd9\u6837\u66f4\u597d\u7406\u89e3\uff0c<code>copy( A, func(B))<\/code> ,\u5728\u5b9e\u4f8b\u5316A\u4e4b\u540e\uff0cA\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8c03\u7528<code>copy<\/code>\u51fd\u6570\uff08\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff09\uff0c\u4f46\u662f\u8be5\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u800c\u5b83\u9700\u8981\u88ab<code>func()<\/code>\u51fd\u6570\u8fd4\u56de\uff0c\u6240\u4ee5\u9700\u8981\u518d\u8c03\u7528<code>func<\/code>\u5bf9\u83b7\u53d6\u7b2c\u4e8c\u4e2a\u53c2\u6570\uff0c\u5b9e\u9645\u4e0a\u5b83\u4eec\u4f9d\u65e7\u5728\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u7684\u4e16\u754c\u91cc\uff0c\u4e0d\u8fc7\u7531\u4e8e\u7b2c\u4e8c\u4e2a\u53c2\u6570\u9700\u8981\u8fdb\u884c\u4e00\u4e2a\u83b7\u53d6\u64cd\u4f5c\uff0c\u9020\u6210\u4e86\u7c7b\u7684\u5b9e\u4f8b\u5316\u548c\u8c03\u7528\u6784\u9020\u51fd\u6570\u88ab\u6253\u65ad\u7684\u9519\u89c9\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n<\/details>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u9664\u4e86\u8fd9\u4e24\u4e2a\u5927\u7c7b\u5916\uff0c\u6bcf\u4e00\u7c7b\u90fd\u53ef\u4ee5\u7ec6\u5206\u4e3a\u73b0\u5b9e\u8c03\u7528\u6784\u9020\u51fd\u6570\u548c\u9690\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u666e\u901a\u6784\u9020\u51fd\u6570\u4e2d\uff0c\u901a\u5e38\u663e\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u7684\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class a;\/\/\u9ed8\u8ba4\u6784\u9020\nmy_class a(10);\/\/\u6709\u53c2\u6784\u9020<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u9690\u5f0f\u6784\u9020\u51fd\u6570\u901a\u8fc7\u5982\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class a = 10;\/\/\u6709\u53c2\u6784\u9020<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u62f7\u8d1d\u6784\u9020\u6070\u6070\u76f8\u53cd\uff0c\u663e\u5f0f\u7684\u62f7\u8d1d\u6784\u9020\u662f\u8fd9\u6837\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class a(10);\/\/\u521d\u59cb\u5316\u4e00\u4e2a\u793a\u4f8b\nmy_class b(a);\/\/\u5c06\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4f5c\u4e3a\u53c2\u6570\u521b\u5efa\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u9690\u5f0f\u5219\u662f\u6bd4\u8f83\u5e38\u7528\u7684\u8fd9\u79cd\u5f62\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class a(10);\/\/\u521d\u59cb\u5316\u4e00\u4e2a\u793a\u4f8b\nmy_class b = a;\/\/\u5c06\u7b2c\u4e00\u4e2a\u793a\u4f8b\u4f5c\u4e3a\u53c2\u6570\u521b\u5efa\u7b2c\u4e8c\u4e2a\u5b9e\u4f8b<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u6790\u6784\u51fd\u6570<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728c\u8bed\u8a00\u4e2d\uff0c\u5e38\u5e38\u4f1a\u5bfc\u81f4\u5404\u79cd\u8d44\u6e90\u5fd8\u8bb0\u56de\u6536\u7684\u56e7\u5883\uff0c\u6bd4\u5982\u7531\u4e8e\u8c03\u7528\u4e86\u52a8\u6001\u5185\u5b58\u5206\u914d\u5374\u5fd8\u8bb0\u91ca\u653e\u5185\u5b58\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u9732\uff0c\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e00\u4e2a\u95ee\u9898\uff0cc++\u5f15\u5165\u4e86\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u2014\u2014\u6790\u6784\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6790\u6784\u51fd\u6570\u662fC++\u7c7b\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u6210\u5458\u51fd\u6570\uff0c\u5b83\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\u81ea\u52a8\u8c03\u7528\uff0c\u7528\u4e8e\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u4f8b\u5982\u91ca\u653e\u8d44\u6e90\u3001\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u5185\u5b58\u7b49\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6790\u6784\u51fd\u6570\u5728C++\u4e2d\u662f\u4e0d\u80fd\u88ab\u91cd\u8f7d\u7684\u3002\u6790\u6784\u51fd\u6570\u7684\u7b7e\u540d\u662f\u56fa\u5b9a\u7684\uff0c\u5b83\u6ca1\u6709\u8fd4\u56de\u7c7b\u578b\uff0c\u4e5f\u4e0d\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002\u4e3b\u8981\u539f\u56e0\u662f\uff1a\u6790\u6784\u51fd\u6570\u7684\u4e3b\u8981\u76ee\u7684\u662f\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u7ed3\u675f\u65f6\u6267\u884c\u6e05\u7406\u64cd\u4f5c\uff0c\u4f8b\u5982\u91ca\u653e\u8d44\u6e90\u3001\u5173\u95ed\u6587\u4ef6\u3001\u91ca\u653e\u5185\u5b58\u7b49\u3002\u7531\u4e8e\u6790\u6784\u51fd\u6570\u662f\u5728\u5bf9\u8c61\u88ab\u9500\u6bc1\u65f6\u81ea\u52a8\u8c03\u7528\u7684\uff0c\u56e0\u6b64\u5b83\u4e0d\u9700\u8981\u4e5f\u4e0d\u80fd\u63a5\u53d7\u4efb\u4f55\u53c2\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nclass my_class{\nprivate:\n    int* p = NULL;\npublic:\n    my_class(int a);\n    ~my_class();\n};\nmy_class::my_class(int a){\n    this-&gt;p = new int;\n    *this-&gt;p = a;\n    cout&lt;&lt;\"\u521b\u5efa\u7c7b\"&lt;&lt;endl;\n}\nmy_class::~my_class(){\n    delete this-&gt;p;\n    this-&gt;p = NULL;\n    cout&lt;&lt;\"\u7c7b\u91ca\u653e\"&lt;&lt;endl;\n}\nvoid func(){\n    my_class a(10);\n    return;\n}\nint main(){\n    func();\n    cout&lt;&lt;\"main\u51fd\u6570\u7ed3\u675f\"&lt;&lt;endl;\n    return 0;\n}   <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u521b\u5efa\u7c7b\n\u7c7b\u91ca\u653e\nmain\u51fd\u6570\u7ed3\u675f<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u89e3\u6790\">\u62f7\u8d1d\u6784\u9020\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u662fC++\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a<mark style=\"background-color:#f78da7\" class=\"has-inline-color\">\u6784\u9020\u51fd\u6570\uff08\u6700\u5bb9\u6613\u5ffd\u7565\uff09<\/mark>\uff0c\u7528\u4e8e\u521b\u5efa\u4e00\u4e2a\u65b0\u5bf9\u8c61\u4f5c\u4e3a\u73b0\u6709\u5bf9\u8c61\u7684\u526f\u672c\u3002\u5b83\u5728\u4ee5\u4e0b\u60c5\u51b5\u4e0b\u88ab\u8c03\u7528\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u901a\u8fc7\u503c\u4f20\u9012\u5bf9\u8c61<\/strong>\uff1a\u5f53\u5bf9\u8c61\u4f5c\u4e3a\u53c2\u6570\u4f20\u9012\u7ed9\u51fd\u6570\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u901a\u8fc7\u503c\u8fd4\u56de\u5bf9\u8c61<\/strong>\uff1a\u5f53\u5bf9\u8c61\u4f5c\u4e3a\u51fd\u6570\u7684\u8fd4\u56de\u503c\u65f6\u3002<\/li>\n\n\n\n<li><strong>\u663e\u5f0f\u590d\u5236\u5bf9\u8c61<\/strong>\uff1a\u5f53\u4f7f\u7528\u62f7\u8d1d\u521d\u59cb\u5316\uff08\u4f8b\u5982&nbsp;<code>MyClass obj2 = obj1;<\/code>\uff09\u65f6\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color has-black-color\">\u6d45\u62f7\u8d1d<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u663e\u5f0f\u5b9a\u4e49\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u7f16\u8bd1\u5668\u4f1a\u4e3a\u4f60\u751f\u6210\u4e00\u4e2a\u9ed8\u8ba4\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\u9ed8\u8ba4\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6267\u884c\u6d45\u62f7\u8d1d\uff0c\u5373\u9010\u6210\u5458\u590d\u5236\u5bf9\u8c61\u7684\u6240\u6709\u6210\u5458\u53d8\u91cf\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u6df1\u62f7\u8d1d<\/mark><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6d45\u62f7\u8d1d\u53ef\u80fd\u4e0d\u591f\u7528\uff0c\u4f8b\u5982\u5f53\u7c7b\u5305\u542b\u6307\u5411\u5806\u533a\u7684\u6307\u9488\u65f6\u3002\u6b64\u65f6\uff0c\u4f60\u9700\u8981\u5b9a\u4e49\u4e00\u4e2a\u81ea\u5b9a\u4e49\u7684\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6765\u6267\u884c\u6df1\u62f7\u8d1d\u3002\u6df1\u62f7\u8d1d\u4f1a\u590d\u5236\u6307\u9488\u6240\u6307\u5411\u7684\u5b9e\u9645\u6570\u636e\uff0c\u800c\u4e0d\u4ec5\u4ec5\u662f\u6307\u9488\u672c\u8eab\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6df1\u62f7\u8d1d\u7684\u6807\u51c6\u683c\u5f0f<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u7c7b\u540d( const \u7c7b\u540d&amp; \u53c2\u6570\u540d)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nclass my_class{\nprivate:\n    int* a = nullptr;\npublic:\n    my_class(int a);\n    my_class(const my_class&amp; other);\n    ~my_class();\n    void print();\n};\nmy_class::my_class(int a){\n    this-&gt;a = new int;\n    *this-&gt;a = a;\n}\nmy_class::my_class(const my_class&amp; other){\n    this-&gt;a = new int;\n    *this-&gt;a = *other.a;\n}\nmy_class::~my_class(){\n    delete this-&gt;a;\n    this-&gt;a = nullptr;\n}\nvoid my_class::print(){\n    cout&lt;&lt;*this-&gt;a&lt;&lt;\" &#91;\"&lt;&lt;this-&gt;a&lt;&lt;\"]\"&lt;&lt;endl;\n}\nint main(){\n    my_class a(10);\n    a.print();\n    my_class b = a;\n    b.print();\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>10 &#91;0x6503fbf7deb0]\n10 &#91;0x6503fbf7e2e0]<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u5206\u6790\uff1a<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\u53ef\u4ee5\u770b\u5230\uff0c\u4e24\u4e2a\u5bf9\u8c61\u7684\u503c\u867d\u7136\u76f8\u7b49\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a\u5b83\u4eec\u7684\u5730\u5740\u662f\u4e0d\u540c\u7684\uff0c\u4e5f\u4e0d\u80fd\u76f8\u540c\u3002\u56e0\u4e3a\u5b83\u4eec\u5bf9\u5e94\u4e86\u4e24\u4e2a\u4e0d\u540c\u7684\u52a8\u6001\u5185\u5b58\u7a7a\u95f4\uff0c\u5982\u679c\u76f8\u540c\uff0c\u5219\u4f1a\u5bfc\u81f4\u6570\u636e\u51b2\u7a81\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b91<\/strong>\uff1a\u4e3a\u4ec0\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u683c\u5f0f\u4e2d\u5fc5\u987b\u8981\u6709 <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">\u5f15\u7528\u7b26\u53f7<\/mark> <code>&amp;<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8981\u6ce8\u610f\u5230\u4e00\u70b9\uff0c\u62f7\u8d1d\u51fd\u6570\u53d1\u751f\u5728\u6570\u636e\u62f7\u8d1d\u7684\u65f6\u5019\uff0c\u5982\u679c\u4e0d\u4f7f\u7528\u5f15\u7528\uff0c\u5c31\u4f1a\u9020\u6210\u4e00\u4e2a\u6b7b\u5faa\u73af\uff1a\u5982\u679c\u4e0d\u4f7f\u7528\u5f15\u7528\uff0c\u90a3\u4e48\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u53d1\u751f\u62f7\u8d1d\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u628a\u5916\u754c\u7684\u53c2\u6570\u62f7\u8d1d\u5230\u81ea\u5df1\u7684\u53c2\u6570\u4e2d\uff0c\u7531\u4e8e\u5916\u754c\u5c06\u9700\u8981\u5c06\u53c2\u6570\u62f7\u8d1d\u5230\u5f62\u53c2\u4e0a\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u8c03\u7528\u62f7\u8d1d\u51fd\u6570&#8230;.\u7136\u540e\u5c31\u6b7b\u5faa\u73af\u4e86\u2014\u2014\u4e3a\u4e86\u62f7\u8d1d\u6211\u9700\u8981\u5c06\u53c2\u6570\u62f7\u8d1d\u5230\u6211\u7684\u5f62\u53c2\u91cc\u518d\u8fdb\u884c\u62f7\u8d1d\uff0c\u90a3\u4e48\u600e\u4e48\u628a\u503c\u62f7\u8d1d\u8fdb\u6211\u7684\u5f62\u53c2\u91cc\u5462\uff1f\u90a3\u5c31\u4f7f\u7528\u62f7\u8d1d\u51fd\u6570\uff0c\u5b8c\u7f8e\u7684\u903b\u8f91\u95ed\u73af\u3002\u6240\u4ee5\u4ece\u4e0a\u8ff0\u60c5\u51b5\u5c31\u80fd\u660e\u786e\u7684\u77e5\u9053\uff0c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u5fc5\u987b\u4f7f\u7528\u5f15\u7528\u5f62\u53c2\u7684\u65b9\u5f0f\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b92\uff1a<\/strong>\u4f20\u5165\u7684\u53c2\u6570\u4e0d\u662f\u4e00\u4e2a\u7c7b\u7684\u5b9e\u4f8b\u5417\uff1f\u7c7b\u7684\u5b9e\u4f8b\u4e3a\u4ec0\u4e48\u53ef\u4ee5\u8bbf\u95ee\u79c1\u6709\u6210\u5458\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u8fd9\u91cc\u6709\u4e00\u4e2a\u5f88\u591a\u4eba\u90fd\u6709\u7684\u8bef\u533a\uff0c\u7c7b\u7684\u5c01\u88c5\u9488\u5bf9\u7684\u662f\u7c7b\uff0c\u4e5f\u5c31\u8bf4\uff0c\u5916\u754c\u4e0d\u80fd\u8bbf\u95ee\u7c7b\u7684\u5185\u90e8\uff0c\u4e0d\u540c\u7c7b\u4e4b\u95f4\u4e0d\u80fd\u8bbf\u95ee\u7c7b\u7684\u5185\u90e8\u3002\u4f46\u662f\u540c\u4e00\u4e2a\u7c7b\u7684\u4e0d\u540c\u5b9e\u4f8b\uff08\u5bf9\u8c61\uff09\uff0c\u662f\u53ef\u4ee5\u76f8\u4e92\u8bbf\u95ee\u79c1\u6709\u6210\u5458\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b93\uff1a<\/strong>\u8d4b\u503c\u64cd\u4f5c\u51fd\u6570\u548c\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u6709\u4ec0\u4e48\u533a\u522b\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u4e00\u4e2a\u975e\u5e38\u5bb9\u6613\u88ab\u8bef\u89e3\u7684\u70b9\uff0c\u56e0\u4e3a\u4e24\u8005\u90fd\u6d89\u53ca\u5230\u4e86<code> \u201c=\u201d <\/code>\u7b26\u53f7\uff0c\u4f46\u662f\u4e24\u8005\u5b9e\u9645\u4e0a\u6709\u8d28\u7684\u533a\u522b\uff0c\u6838\u5fc3\u662f\u51fd\u6570\u8c03\u7528\u7684\u65f6\u673a\uff0c\u90a3\u4e48\u8981\u5982\u4f55\u533a\u5206\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9996\u5148\u8981\u7406\u89e3\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u5b83\u662f\u6784\u9020\u51fd\u6570\u7684\u4e00\u7c7b\uff0c\u6784\u9020\u51fd\u6570\u662f\u5728\u7c7b\u8fdb\u884c\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\u5bf9\u7c7b\u8fdb\u884c\u521d\u59cb\u5316\u64cd\u4f5c\u7684\uff0c\u90a3\u4e48\u5b83\u51fa\u73b0\u7684\u65f6\u95f4\u70b9\u4e00\u5b9a\u662f\u53d1\u751f\u4e86\u7c7b\u7684\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u8d4b\u503c\u64cd\u4f5c\u51fd\u6570\u7684\u6838\u5fc3\u662f\u8d4b\u503c\uff0c\u8d4b\u503c\u5c31\u610f\u5473\u7740\u5f53\u524d\u5df2\u7ecf\u5b58\u5728\u4e86\u4e00\u4e2a\u5bf9\u8c61\uff0c\u53ea\u6709\u5bf9\u8c61\u5b58\u5728\u624d\u80fd\u8fdb\u884c\u8d4b\u503c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class func(){\n    my_class a;\n    return a;\n}\nint main(){\n    my_class b;\n    b = func();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f=\u7b26\u53f7\u662f\u8d4b\u503c\uff0c\u56e0\u4e3ab\u662f\u4e00\u4e2a\u5df2\u7ecf\u5b58\u5728\u7684\u5bf9\u8c61\uff0c\u8d4b\u503c\u662f\u5bf9\u5bf9\u8c61\u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>my_class func(){\n    my_class a(10);\n    return a;\n}\n\nint main(){\n    my_class b = func();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u62f7\u8d1d\u6784\u9020\uff0cb\u662f\u4e00\u4e2a\u4e0d\u5b58\u5728\u7684\u5b9e\u4f53\uff0c\u5b83\u9700\u8981\u8fdb\u884c\u5b9e\u4f8b\u5316\uff0c\u5b9e\u4f8b\u5316\u9700\u8981\u6784\u9020\u51fd\u6570\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"\u9759\u6001\u6210\u5458\u51fd\u6570\u89e3\u6790\">\u9759\u6001\u6210\u5458\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u9759\u6001\u6210\u5458\u51fd\u6570\u662f C++ \u7c7b\u4e2d\u7684\u4e00\u4e2a\u91cd\u8981\u6982\u5ff5\u3002\u9759\u6001\u6210\u5458\u51fd\u6570\u975e\u5e38\u7279\u6b8a\uff0c\u5b83\u4e0d\u4e0e\u4efb\u4f55\u5bf9\u8c61\u5b9e\u4f8b\u5173\u8054\uff0c\u53ea\u5c5e\u4e8e\u7c7b\u672c\u8eab\uff0c\u4e5f\u5c31\u610f\u5473\u7740\u5b83\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528\u7c7b\u7684this\u6307\u9488\uff0c\u5b83\u65e0\u6cd5\u8bbf\u95ee\u7c7b\u4e2d\u9664\u4e86\u9759\u6001\u6210\u5458\u53d8\u91cf\u4ee5\u5916\u7684\u6210\u5458\u3002\u800c\u6b63\u662f\u5b83\u4e0d\u4e0e\u4efb\u4f55\u5b9e\u4f8b\u5173\u8054\uff0c\u6240\u4ee5\u5b83\u6709\u4e00\u4e2a\u5f88\u91cd\u8981\u7684\u7279\u6027\u2014\u2014\u516c\u6709\u6743\u9650\u4e0b\u5b83\u53ef\u4ee5\u4e0d\u7ecf\u8fc7\u5b9e\u4f8b\u5316\u76f4\u63a5\u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u793a\u4f8b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MyClass::func(); \/\/ \u901a\u8fc7\u7c7b\u540d\u8c03\u7528<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0e\u9759\u6001\u6210\u5458\u9ad8\u5ea6\u76f8\u5173\u7684\u662f<mark style=\"background-color:#f78da7\" class=\"has-inline-color\">\u9759\u6001\u6210\u5458\u53d8\u91cf<\/mark>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u9759\u6001\u6210\u5458\u53d8\u91cf\u548c\u5177\u6709\u9759\u6001\u53d8\u91cf\u7684\u7279\u6027\uff0c\u4e0d\u8fc7\u5b83\u66f4\u52a0\u7279\u6b8a\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u5b83\u4e0d\u53ef\u4ee5\u5728\u7c7b\u4e2d\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li>\u5b83\u4e0d\u5360\u7528\u5bf9\u8c61\u7684\u5185\u5b58\u3002<\/li>\n\n\n\n<li>\u516c\u6709\u6743\u9650\u4e0b\u5b83\u4e0d\u9700\u8981\u7c7b\u88ab\u5b9e\u4f8b\u5316\u4e5f\u80fd\u76f4\u63a5\u88ab\u8bbf\u95ee\u3002<\/li>\n\n\n\n<li>\u6240\u6709\u7684\u7c7b\u7684\u5b9e\u4f8b\u5171\u4eab\u4e00\u4e2a\u9759\u6001\u53d8\u91cf<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass MyClass {\npublic:\n    static int count; \/\/ \u9759\u6001\u6210\u5458\u53d8\u91cf\n\n    MyClass() {\n        count++; \/\/ \u6bcf\u521b\u5efa\u4e00\u4e2a\u5bf9\u8c61\uff0ccount \u589e\u52a0\n    }\n    ~MyClass() {\n        count--; \/\/ \u6bcf\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61\uff0ccount\u51cf\u5c11\n        cout&lt;&lt;\"\u91ca\u653e count = \"&lt;&lt;count&lt;&lt;endl;\n    }\n    static void displayCount() { \/\/ \u9759\u6001\u6210\u5458\u51fd\u6570\n        cout &lt;&lt; \"Count: \" &lt;&lt; count &lt;&lt; endl;\n    }\n};\n\n\/\/ \u5728\u7c7b\u5916\u90e8\u521d\u59cb\u5316\u9759\u6001\u6210\u5458\u53d8\u91cf\nint MyClass::count = 0;\n\nint main() {\n    MyClass obj1; \/\/ \u521b\u5efa\u7b2c\u4e00\u4e2a\u5bf9\u8c61\n    \/\/ \u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\n    MyClass::displayCount(); \n\n    MyClass obj2; \/\/ \u521b\u5efa\u7b2c\u4e8c\u4e2a\u5bf9\u8c61\n    \/\/ \u901a\u8fc7\u7c7b\u540d\u8c03\u7528\u9759\u6001\u6210\u5458\u51fd\u6570\n    MyClass::displayCount(); \n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Count: 1\nCount: 2\n\u91ca\u653e count = 1\n\u91ca\u653e count = 0<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e38\u91cf\u6210\u5458\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u91cf\u6210\u5458\u51fd\u6570\u662f\u6307\u5728\u51fd\u6570\u58f0\u660e\u540e\u9762\u52a0\u4e0a&nbsp;<code>const<\/code>&nbsp;\u5173\u952e\u5b57\u7684\u6210\u5458\u51fd\u6570\u3002\u5b83\u4eec\u4e0d\u80fd\u4fee\u6539\u5bf9\u8c61\u7684\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff08\u9664\u4e86&nbsp;<code>mutable<\/code>&nbsp;\u53d8\u91cf\uff09\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e00\u4e2a\u5178\u578b\u7684\u5f62\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>void func() const;{\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b83\u7684\u7279\u70b9\u5c31\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u53ea\u80fd\u8c03\u7528\u5176\u4ed6\u5e38\u91cf\u6210\u5458\u51fd\u6570<\/strong>\uff1a\u5e38\u91cf\u6210\u5458\u51fd\u6570\u53ea\u80fd\u8c03\u7528\u5176\u4ed6\u5e38\u91cf\u6210\u5458\u51fd\u6570\uff0c\u4e0d\u80fd\u8c03\u7528\u975e\u5e38\u91cf\u6210\u5458\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5b83\u53ea\u80fd\u8c03\u7528\u7c7b\u4e2d\u5176\u4ed6\u88abconst\u4fee\u9970\u7684\u6210\u5458\u51fd\u6570\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u80fd\u4fee\u6539\u6210\u5458\u53d8\u91cf<\/strong>\uff1a\u5e38\u91cf\u6210\u5458\u51fd\u6570\u4e0d\u80fd\u4fee\u6539\u5bf9\u8c61\u7684\u4efb\u4f55\u6210\u5458\u53d8\u91cf\uff0c\u9664\u4e86\u88ab\u58f0\u660e\u4e3a&nbsp;<code>mutable<\/code>&nbsp;\u7684\u53d8\u91cf\u3002<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f8\u5bf9\u7684\u8fd8\u6709\u5e38\u91cf\u6210\u5458\u53d8\u91cf\uff0c\u5176\u7279\u70b9\u662f\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u521d\u59cb\u5316<\/strong>\uff1a\u5e38\u91cf\u6210\u5458\u53d8\u91cf\u5fc5\u987b\u5728\u6784\u9020\u51fd\u6570\u7684\u521d\u59cb\u5316\u5217\u8868\u4e2d\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u4e0d\u80fd\u5728\u7c7b\u7684\u5176\u4ed6\u5730\u65b9\u8fdb\u884c\u8d4b\u503c\u3002<\/li>\n\n\n\n<li><strong>\u4e0d\u53ef\u4fee\u6539<\/strong>\uff1a\u4e00\u65e6\u521d\u59cb\u5316\uff0c\u5e38\u91cf\u6210\u5458\u53d8\u91cf\u7684\u503c\u4e0d\u80fd\u88ab\u4fee\u6539\u3002<\/li>\n\n\n\n<li><strong>\u53ea\u8bfb\u5c5e\u6027<\/strong>\uff1a\u5e38\u91cf\u6210\u5458\u53d8\u91cf\u63d0\u4f9b\u4e86\u53ea\u8bfb\u5c5e\u6027\uff0c\u786e\u4fdd\u5176\u503c\u5728\u5bf9\u8c61\u7684\u751f\u547d\u5468\u671f\u5185\u4fdd\u6301\u4e0d\u53d8\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\uff0c\u6700\u91cd\u8981\u7684\u4e00\u70b9\u4e0a\u9762\u7684\u7b2c\u4e00\u70b9\u63d0\u5230\u4e86\uff0c\u53ea\u80fd\u5728\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5728\u5b9a\u4e49\u7684\u65f6\u5019\uff0c\u662f\u4e0d\u80fd\u521d\u59cb\u5316\u7684\uff08\u521d\u59cb\u5316\u5217\u8868\u89c1&#8211;&gt;<a href=\"#\u521d\u59cb\u5316\u5217\u8868\u89e3\u6790\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark><\/a>\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6269\u5c55<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u5355\u4f8b\u5316<\/mark><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u5355\u4f8b\u6a21\u5f0f\uff08Singleton Pattern\uff09\u662f\u4e00\u79cd\u8bbe\u8ba1\u6a21\u5f0f\uff0c\u65e8\u5728\u786e\u4fdd\u4e00\u4e2a\u7c7b\u53ea\u6709\u4e00\u4e2a\u5b9e\u4f8b\uff0c\u5e76\u63d0\u4f9b\u4e00\u4e2a\u5168\u5c40\u8bbf\u95ee\u70b9\u6765\u8bbf\u95ee\u8be5\u5b9e\u4f8b\u3002\u5355\u4f8b\u6a21\u5f0f\u5728\u9700\u8981\u63a7\u5236\u8d44\u6e90\u8bbf\u95ee\u6216\u7ba1\u7406\u5168\u5c40\u72b6\u6001\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u79cd\u65b9\u5f0f\u7684\u6838\u5fc3\u4e3b\u8981\u662f\u4f9d\u8d56\u8bbf\u95ee\u6743\u9650\u548c\u9759\u6001\u6210\u5458\uff08\u9759\u6001\u6210\u5458\u8be6\u89e3&#8211;&gt;<a href=\"#\u9759\u6001\u6210\u5458\u51fd\u6570\u89e3\u6790\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-cyan-blue-color\">\u8df3\u8f6c<\/mark><\/a>\uff09\u7684\u7279\u6027\uff0c\u4ee5\u4e0b\u4e3e\u4e00\u4e2a\u4f46\u662f\u5b9e\u73b0\u7684\u65b9\u5f0f\uff0c\u6ce8\u610f\u5b9e\u73b0\u65b9\u5f0f\u5e76\u4e0d\u662f\u552f\u4e00\u7684\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\n\/\/\u5355\u4f8b\u7c7b\nclass Singleton {\nprivate:\n    int a;\n    \/\/ \u79c1\u6709\u5316\u6784\u9020\u51fd\u6570\uff0c\u8ba9\u5176\u53ea\u5141\u8bb8\u5185\u90e8\u8c03\u7528\n    Singleton(int a) {this-&gt;a = a;}\n    \/\/ \u5220\u9664\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26\uff0c\u6ce8\u610f\u8fd9\u91cc\u7684\u7684\u7279\u4e86\u4e0d\u662f\u91ca\u653e\u52a8\u6001\u5185\u5b58\uff0c\u800c\u662f\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u4e0d\u5141\u8bb8\u8fdb\u884c\u8be5\u64cd\u4f5c\uff08\u8fd9\u79cd\u8bed\u6cd5\u5728c++11\u540e\u88ab\u5f15\u5165\uff09\u3002 \n    Singleton(const Singleton&amp;) = delete;       \/\/\u7981\u7528\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u3002\n    Singleton&amp; operator=(const Singleton&amp;) = delete;    \/\/\u7981\u7528\u8d4b\u503c\u8fd0\u7b97\u7b26\u3002\npublic:\n    \/\/ \u63d0\u4f9b\u4e00\u4e2a\u9759\u6001\u65b9\u6cd5\u6765\u83b7\u53d6\u5b9e\u4f8b\n    static Singleton&amp; getInstance(int a) {\n        static Singleton instance(a);\n        return instance;\n    }\n    \/\/\u64cd\u4f5c\n    void func() {\n        cout&lt;&lt;\"\u8fd9\u91cc\u662f\u5355\u4f8b\u7c7b,a = \"&lt;&lt;this-&gt;a&lt;&lt;endl;\n    }\n};\nint main(){\n    Singleton&amp; instance = Singleton::getInstance(10);\n    instance.func();\n\n    Singleton&amp; instance2 = Singleton::getInstance(100);\n    instance2.func();\n}   <\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fd9\u91cc\u662f\u5355\u4f8b\u7c7b,a = 10\n\u8fd9\u91cc\u662f\u5355\u4f8b\u7c7b,a = 10<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u4f1a\u53d1\u73b0\u521b\u5efa\u7684\u4e24\u4e2a\u503c\u5b9e\u9645\u4e0a\u662f\u4e00\u4e2a\u5b9e\u4f8b\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"\u521d\u59cb\u5316\u5217\u8868\u89e3\u6790\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">\u521d\u59cb\u5316\u5217\u8868<\/mark><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u521d\u59cb\u5316\u5217\u8868\u662f C++ \u4e2d\u7528\u4e8e\u5728<mark style=\"background-color:#f78da7\" class=\"has-inline-color\">\u6784\u9020\u51fd\u6570<\/mark>\u4e2d\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\u7684\u4e00\u79cd\u65b9\u5f0f\u3002\u5b83\u5141\u8bb8\u5728\u6784\u9020\u51fd\u6570\u4f53\u6267\u884c\u4e4b\u524d\u5bf9\u6210\u5458\u53d8\u91cf\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5176\u5177\u6709\u4ee5\u4e0b\u7279\u70b9\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9ad8\u6548<\/strong>\uff1a\u521d\u59cb\u5316\u5217\u8868\u76f4\u63a5\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\uff0c\u907f\u514d\u4e86\u5728\u6784\u9020\u51fd\u6570\u4f53\u5185\u8fdb\u884c\u8d4b\u503c\u64cd\u4f5c\uff0c\u4ece\u800c\u63d0\u9ad8\u4e86\u6548\u7387\u3002<\/li>\n\n\n\n<li><strong>\u5fc5\u987b\u6027<\/strong>\uff1a\u5bf9\u4e8e\u5e38\u91cf\u6210\u5458\u53d8\u91cf\u3001\u5f15\u7528\u6210\u5458\u53d8\u91cf\u548c\u6ca1\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u7684\u7c7b\u7c7b\u578b\u6210\u5458\u53d8\u91cf\uff0c\u5fc5\u987b\u4f7f\u7528\u521d\u59cb\u5316\u5217\u8868\u8fdb\u884c\u521d\u59cb\u5316\u3002<\/li>\n\n\n\n<li><strong>\u987a\u5e8f<\/strong>\uff1a\u521d\u59cb\u5316\u5217\u8868\u7684\u521d\u59cb\u5316\u987a\u5e8f\u4e0e\u6210\u5458\u53d8\u91cf\u5728\u7c7b\u4e2d\u58f0\u660e\u7684\u987a\u5e8f\u4e00\u81f4\uff0c\u800c\u4e0d\u662f\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u7684\u987a\u5e8f\u3002<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u521d\u59cb\u5316\u5217\u8868\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class MyClass {\npublic:\n    int a;\n    const int b;\n    int&amp; c;\n\n    MyClass(int x, int y, int&amp; z) : a(x), b(y), c(z) {\n        \/\/ \u6784\u9020\u51fd\u6570\u4f53\n    }\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6216\u8005\u8bf4<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class ClassName {\npublic:\n    ClassName(\u53c2\u6570\u5217\u8868) : \u6210\u5458\u53d8\u91cf1(\u521d\u59cb\u503c1), \u6210\u5458\u53d8\u91cf2(\u521d\u59cb\u503c2), ... {\n        \/\/ \u6784\u9020\u51fd\u6570\u4f53\n    }\nprivate:\n    \u6210\u5458\u53d8\u91cf\u7c7b\u578b1 \u6210\u5458\u53d8\u91cf1;\n    \u6210\u5458\u53d8\u91cf\u7c7b\u578b2 \u6210\u5458\u53d8\u91cf2;\n    ...\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass MyClass {\npublic:\n    int a;\n    const int b;\n    int&amp; c;\n\n    \/\/ \u6784\u9020\u51fd\u6570\uff0c\u5728\u521d\u59cb\u5316\u5217\u8868\u4e2d\u521d\u59cb\u5316\u6210\u5458\u53d8\u91cf\n    MyClass(int x, int y, int&amp; z) : a(x), b(y), c(z) {}\n\n    void display() const {\n        cout &lt;&lt; \"a: \" &lt;&lt; a &lt;&lt; \", b: \" &lt;&lt; b &lt;&lt; \", c: \" &lt;&lt; c &lt;&lt; endl;\n    }\n};\n\nint main() {\n    int ref = 30;\n    MyClass obj(10, 20, ref);\n    obj.display(); \n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>a: 10, b: 20, c: 30<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u521d\u59cb\u5316\u5217\u8868\u662f\u5c5e\u4e8e\u6784\u9020\u51fd\u6570\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5b8c\u6574\u7684\u6784\u9020\u51fd\u6570\u770b\u4f5c\u4e09\u90e8\u5206\uff08\u4ece\u6c47\u7f16\u7684\u89d2\u5ea6\uff09\uff0c\u7b2c\u4e00\u90e8\u5206\u662f\u6784\u9020\u51fd\u6570\u6807\u7b7e\uff0c\u7b2c\u4e8c\u90e8\u5206\u5c31\u662f\u521d\u59cb\u5316\u5217\u8868\u7684\u6267\u884c\uff0c\u7b2c\u4e09\u90e8\u5206\u5219\u662f\u6784\u9020\u51fd\u6570\u7684\u51fd\u6570\u4f53\u3002\u4e09\u4e2a\u90e8\u5206\u4ece\u4e0a\u5f80\u4e0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u6ca1\u6709\u521d\u59cb\u5316\u5217\u8868\u7684\u60c5\u51b5\u4e0b\u5c31\u6ca1\u6709\u6c47\u7f16\u4ee3\u7801\uff0c\u81ea\u7136\u800c\u7136\u6807\u7b7e\u4e0b\u9762\u7684\u5c31\u662f\u51fd\u6570\u4f53\u4e86\u3002<\/p>\n\n\n\n<details class=\"wp-block-details\"><summary><mark style=\"background-color:#ff6900\" class=\"has-inline-color is-layout-flow wp-block-details-is-layout-flow\">\u5bf9\u4e8e\u5217\u8868\u521d\u59cb\u5316\u8fd8\u5b58\u5728\u4e00\u4e2a\u7591\u70b9\uff1a<\/mark><\/summary>\n<p class=\"wp-block-paragraph\">\u9996\u5148\u662f\u5217\u8868\u521d\u59cb\u5316\u7684\u5b9a\u4e49\u662f[ ClassName(\u53c2\u6570\u5217\u8868) : \u6210\u5458\u53d8\u91cf1(\u521d\u59cb\u503c1), \u6210\u5458\u53d8\u91cf2(\u521d\u59cb\u503c2),&#8230; ] \u3002\u8fd9\u4e2a\u5f88\u597d\u7406\u89e3\uff0c\u5c31\u662f\u5c06\u62ec\u53f7\u5185\u90e8\u7684\u503c\u4f20\u5165\u6210\u5458\u53d8\u91cf\u4e2d\uff0c\u5373\u4fbf\u6210\u5458\u66ff\u6362\u6210\u7c7b\u5bf9\u8c61\u4e5f\u5f88\u597d\u7406\u89e3\uff0c\u56e0\u4e3a\u5bf9\u8c61\u540d\uff0b\u503c\uff0c\u8fd9\u4e2a\u662f\u6784\u9020\u51fd\u6570\u7684\u4e00\u79cd\u663e\u5f0f\u8c03\u7528\u7684\u65b9\u6cd5\uff0c\u4f46\u662f\u5374\u80fd\u4f7f\u7528\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u76f4\u63a5\u8c03\u7528\u7c7b\u540d\uff08\u53c2\u6570\uff09\u8fd9\u6837\u7684\u5f62\u5f0f\u3002<\/p>\n<\/details>\n\n\n\n<h4 class=\"wp-block-heading\"><mark style=\"background-color:#8ed1fc\" class=\"has-inline-color\">explicit<\/mark><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><code>explicit<\/code>&nbsp;\u5173\u952e\u5b57\u662f C++ \u4e2d\u7528\u4e8e\u4fee\u9970\u6784\u9020\u51fd\u6570\u548c\u8f6c\u6362\u51fd\u6570\u7684\u4e00\u4e2a\u91cd\u8981\u5173\u952e\u5b57\u3002\u5b83\u7684\u4e3b\u8981\u4f5c\u7528\u662f\u9632\u6b62\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\uff0c\u4ece\u800c\u907f\u514d\u6f5c\u5728\u7684\u9519\u8bef\u548c\u610f\u5916\u884c\u4e3a\uff0c\u4e3b\u8981\u7279\u70b9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u9632\u6b62\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5f53\u6784\u9020\u51fd\u6570\u88ab&nbsp;<code>explicit<\/code>&nbsp;\u4fee\u9970\u65f6\uff0c\u8be5\u6784\u9020\u51fd\u6570\u4e0d\u80fd\u7528\u4e8e\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u4e0d\u80fd\u4f7f\u7528\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u3002<\/li>\n\n\n\n<li>\u8fd9\u610f\u5473\u7740\u53ea\u80fd\u901a\u8fc7\u663e\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u800c\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6216\u5176\u4ed6\u9690\u5f0f\u65b9\u5f0f\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9002\u7528\u4e8e\u5355\u53c2\u6570\u6784\u9020\u51fd\u6570<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>explicit<\/code>&nbsp;\u5173\u952e\u5b57\u901a\u5e38\u7528\u4e8e\u4fee\u9970\u53ea\u6709\u4e00\u4e2a\u53c2\u6570\u7684\u6784\u9020\u51fd\u6570\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c<code>explicit MyClass(int x);<\/code>&nbsp;\u8868\u793a\u8be5\u6784\u9020\u51fd\u6570\u53ea\u80fd\u901a\u8fc7\u663e\u5f0f\u8c03\u7528\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u9002\u7528\u4e8e\u8f6c\u6362\u51fd\u6570<\/strong>\uff08C++11 \u5f15\u5165\uff09\uff1a\n<ul class=\"wp-block-list\">\n<li><code>explicit<\/code>&nbsp;\u5173\u952e\u5b57\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4fee\u9970\u8f6c\u6362\u51fd\u6570\uff0c\u4ee5\u9632\u6b62\u9690\u5f0f\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n\n\n\n<li>\u4f8b\u5982\uff0c<code>explicit operator int() const;<\/code>&nbsp;\u8868\u793a\u8be5\u8f6c\u6362\u51fd\u6570\u53ea\u80fd\u901a\u8fc7\u663e\u5f0f\u8c03\u7528\u6765\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass MyClass {\npublic:\n    explicit MyClass(int x) : value(x) {}\n\n    int getValue() const {\n        return value;\n    }\n\nprivate:\n    int value;\n};\n\nvoid printValue(const MyClass&amp; obj) {\n    cout &lt;&lt; \"Value: \" &lt;&lt; obj.getValue() &lt;&lt; endl;\n}\n\nint main() {\n    MyClass obj1(10); \/\/ \u663e\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\n    \/\/ MyClass obj2 = 20; \/\/ \u9519\u8bef\uff1a\u4e0d\u80fd\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\n    printValue(obj1);\n    \/\/ printValue(30); \/\/ \u9519\u8bef\uff1a\u4e0d\u80fd\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>MyClass<\/code>&nbsp;\u7684\u6784\u9020\u51fd\u6570\u88ab&nbsp;<code>explicit<\/code>&nbsp;\u4fee\u9970\uff0c\u56e0\u6b64\u4e0d\u80fd\u901a\u8fc7\u9690\u5f0f\u8f6c\u6362\u6765\u521b\u5efa\u5bf9\u8c61\u3002<\/li>\n\n\n\n<li>\u53ea\u80fd\u901a\u8fc7\u663e\u5f0f\u8c03\u7528\u6784\u9020\u51fd\u6570\u6765\u521b\u5efa&nbsp;<code>MyClass<\/code>&nbsp;\u5bf9\u8c61\uff0c\u4f8b\u5982&nbsp;<code>MyClass obj1(10);<\/code>\u3002<\/li>\n\n\n\n<li>\u4e0d\u80fd\u901a\u8fc7\u8d4b\u503c\u6216\u5176\u4ed6\u9690\u5f0f\u65b9\u5f0f\u6765\u521b\u5efa\u5bf9\u8c61\uff0c\u4f8b\u5982&nbsp;<code>MyClass obj2 = 20;<\/code>&nbsp;\u4f1a\u5bfc\u81f4\u7f16\u8bd1\u9519\u8bef\u3002<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>\u6210\u5458\u53d8\u91cf\u7684\u672c\u8d28<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u4e00\u4e2a\u5c24\u5176\u91cd\u8981\u7684\u70b9\uff0c\u4e8b\u5b9e\u4e0a\uff0c\u4f60\u5199\u7684\u7c7b\u5e76\u4e0d\u662f\u7ed9\u673a\u5668\u7528\u7684\uff0c\u800c\u662f\u7ed9\u7f16\u8bd1\u5668\u770b\u7684\u3002\u4e0e\u4e4b\u800c\u6765\u7684\u4e00\u4e2a\u5bb9\u6613\u5ffd\u7565\u7684\u95ee\u9898\u662f\uff0c\u7c7b\u4e2d\u5b9a\u4e49\u7684\u6210\u5458\u4e0d\u8fc7\u662f\u4e00\u4e2a\u7a7a\u58f3\uff0c\u4e8b\u5b9e\u4e0a\u5b83\u4eec\u5e76\u4e0d\u5b58\u5728\u3002\u4e3e\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nclass A{\npublic:\n    int age;\n    A(int a){\n        this-&gt;age = a;\n    }\n    A(){\n        cout&lt;&lt;\"aaaa\"&lt;&lt;endl;\n    }\n};\nclass B{\npublic:\n    B(int i):f(i){}\nprivate:\n    B f;\n};\nint main(){\n    B b(10);\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cA\u7684\u54ea\u4e2a\u6784\u9020\u51fd\u6570\u4f1a\u6267\u884c\u5462\uff1f\u5982\u679c\u770b\u5230\u4e86\u7c7b\u4e2d\u7684 <code>B f;<\/code>\u6050\u6015\u4f1a\u4ee5\u4e3a\u4f1a\u6267\u884c\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u3002\u4f46\u662f\u4e8b\u5b9e\u4e0a\u5e76\u4e0d\u4f1a\uff0c\u56e0\u4e3a\u5728CPU\u770b\u4e0d\u5230\u7c7b\uff0c\u7c7b\u5728\u7f16\u8bd1\u5668\u4e2d\u88ab\u80a2\u89e3\uff0c\u6240\u6709\u7684\u5185\u5b58\u5b89\u6392\u662f\u7f16\u8bd1\u5668\u751f\u6210\u7684\u3002\u5982\u679c\u4f60\u6267\u884c\u8fd9\u4e2a\u4ee3\u7801\uff0c\u4f60\u4f1a\u53d1\u73b0\u6ca1\u6709\u8f93\u51fa\uff0c\u800c\u5982\u679c\u4f60\u770b\u5230\u4e86\u8fd9\u4e2a\u6c47\u7f16\u7684\u4ee3\u7801\uff0c\u5c31\u4f1a\u660e\u767d\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>main:\n.LFB1740:\n\t.cfi_startproc\n\tendbr64\n\tpushq\t%rbp\n\t.cfi_def_cfa_offset 16\n\t.cfi_offset 6, -16\n\tmovq\t%rsp, %rbp\n\t.cfi_def_cfa_register 6\n\tsubq\t$16, %rsp\n\tmovq\t%fs:40, %rax\n\tmovq\t%rax, -8(%rbp)\n\txorl\t%eax, %eax\n\tleaq\t-12(%rbp), %rax\n        ;\/\/\u8fd9\u91cc\u53ef\u4ee5\u770b\u5230\uff0c\u53ea\u5c06\u4e00\u4e2a\u7acb\u5373\u6570\u4f20\u9012\u7ed9\u5bc4\u5b58\u5668\u540e\uff0c\u5c31\u4f20\u9012\u4e86this\u6307\u9488\uff0c\u63a5\u4e0b\u6765\u76f4\u63a5\u8c03\u7528\u4e86\u6784\u9020\u51fd\u6570\uff0c\u6839\u672c\u6ca1\u6709\u751f\u6210\u6240\u8c13\u7684A\u7c7b\u5bf9\u8c61\uff0c\u6240\u4ee5\u8bf4\uff0c\u5b9e\u9645\u4e0a\u8fd9\u4e2aB\u7c7b\u4e2d\u5b9a\u4e49\u7684A\uff0c\u6839\u672c\u5c31\u6ca1\u6709\uff0c\u800c\u662f\u5728\u5b9e\u4f8b\u5316\u4e3a\u5bf9\u8c61\u7684\u9636\u6bb5\u8fdb\u884c\u7684\u6709\u53c2\u6784\u9020\u3002\n\tmovl\t$10, %esi\n\tmovq\t%rax, %rdi\n\tcall\t_ZN3BC1Ei\n\tmovl\t$0, %eax\n\tmovq\t-8(%rbp), %rdx\n\tsubq\t%fs:40, %rdx\n\tje\t.L5\n\tcall\t__stack_chk_fail@PLT\n.L5:\n\tleave\n\t.cfi_def_cfa 7, 8\n\tret\n\t.cfi_endproc\n.LFE1740:<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6839\u636e\u6c47\u7f16\u80fd\u6e05\u6670\u7684\u770b\u5230\uff0c\u5728\u5b9a\u4e49B\u7c7b\u65f6\uff0cprivate\u4e2d\u7684\u7c7bA\u7684\u5b9e\u4f8b\u5bf9\u8c61\u662f\u4e0d\u5b58\u5728\u7684\uff0c\u90a3\u4e2a\u4f4d\u7f6e\u53ea\u662f\u58f0\u660e\u4e86B\u7c7b\u4f1a\u7528\u5230A\u7c7b\uff0c\u800c\u975e\u8fdb\u884c\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><kbd>\u7ee7\u627f<\/kbd><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u524d\u8a00<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ec0\u4e48\u662f\u7ee7\u627f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u540c\u6743\u9650\u7684\u7ee7\u627f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u7ee7\u627f\u8fd9\u4e2a\u6982\u5ff5\u6709\u4e00\u4e2a\u5e38\u89c1\u7684\u8bef\u533a\uff0c\u6709\u4e9b\u4eba\u4f1a\u8bef\u8ba4\u4e3a\u7236\u7c7b\u88ab\u5b50\u7c7b\u7ee7\u627f\uff0c\u4ec5\u4ec5\u7ee7\u627f\u516c\u6709\u548c\u4fdd\u62a4\u6210\u5458\uff0c\u800c\u79c1\u6709\u6210\u5458\u4e0d\u4f1a\u7ee7\u627f\uff0c\u4f46\u662f\u8fd9\u4e2a\u662f\u9519\u8bef\u7684\u3002\u4e8b\u5b9e\u4e0a\uff0c\u7ee7\u627f\u4f1a\u8ba9\u5b50\u7c7b\u5b8c\u5168\u7ee7\u627f\u7236\u7c7b\u6240\u6709\u7684\u6210\u5458\u5305\u62ec\u79c1\u6709\u6210\u5458\uff0c\u4f46\u662f\u5b50\u7c7b\u662f\u6ca1\u6709\u6743\u9650\u8bbf\u95ee\u7236\u7c7b\u7684\u79c1\u6709\u6210\u5458\u7684\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u7ee7\u627f\u65b9\u5f0f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ee7\u627f\u6743\u9650<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u7ee7\u627f\u6743\u9650\u4e00\u5171\u6709\u4e09\u79cd\uff0c\u5206\u522b\u662f\u516c\u6709\u7ee7\u627f\uff0c\u4fdd\u62a4\u7ee7\u627f\u548c\u79c1\u6709\u7ee7\u627f\uff0c\u6240\u8c13\u7684\u7ee7\u627f\u6743\u9650\u5176\u5b9e\u662f\u5f71\u54cd\u7236\u7c7b\u88ab\u7ee7\u627f\u540e\u5728\u5b50\u7c7b\u4e2d\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u516c\u6709\u7ee7\u627f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u516c\u6709\u7ee7\u627f\u76f8\u5f53\u4e8e\u5c06\u7236\u7c7b\u7684\u539f\u5c01\u4e0d\u52a8\u7684\u7ee7\u627f\u3002\u5982\u679c\u7236\u7c7b\u4e2d\u6210\u5458\u6743\u9650\u662f\u516c\u6709\uff0c\u90a3\u4e48\u5728\u5b50\u7c7b\u4e5f\u516c\u6709\uff0c\u5982\u679c\u662f\u4fdd\u62a4\uff0c\u90a3\u4e48\u5728\u5b50\u7c7b\u4e5f\u662f\u4fdd\u62a4\uff0c\u5982\u679c\u662f\u79c1\u6709\u5728\u5b50\u7c7b\u4e5f\u662f\u79c1\u6709\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class son : public father<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u4fdd\u62a4\u7ee7\u627f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4fdd\u62a4\u7ee7\u627f\u5219\u4f1a\u5c06\u7236\u7c7b\u4e2d\u516c\u6709\u548c\u4fdd\u62a4\u6743\u9650\u7684\u6210\u5458\uff0c\u5728\u5b50\u7c7b\u4e2d\u6743\u9650\u90fd\u8f6c\u5316\u4e3a\u4fdd\u62a4\u6743\u9650\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class son : protected father<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u79c1\u6709\u7ee7\u627f<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u79c1\u6709\u7ee7\u627f\u4f1a\u5c06\u7236\u7c7b\u4e2d\u7684\u516c\u6709\u4e0e\u4fdd\u62a4\u6743\u9650\u7684\u6210\u5458\uff0c\u5728\u5b50\u7c7b\u4e2d\u8f6c\u6362\u4e3a\u79c1\u6709\u6210\u5458\u7684\u6743\u9650\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class son : private father<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u603b\u7ed3<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e09\u79cd\u7ee7\u627f\u65b9\u5f0f\u4e2d\uff0c\u4fdd\u62a4\u7ee7\u627f\u548c\u79c1\u6709\u7ee7\u627f\u4f1a\u6539\u53d8\u6210\u5458\u6743\u9650\uff0c\u4f46\u662f\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u53ea\u80fd\u6539\u53d8\u7236\u7c7b\u4fdd\u62a4\u548c\u516c\u6709\u6743\u9650\uff0c\u5bf9\u4e8e\u7236\u7c7b\u7684\u79c1\u6709\u6743\u9650\uff0c\u662f\u4e0d\u80fd\u88ab\u7ee7\u627f\u65b9\u5f0f\u4fee\u6539\u6743\u9650\u7684\uff0c\u4e0d\u7ba1\u5982\u4f55\u7ee7\u627f\uff0c\u5b83\u90fd\u662f\u88ab\u7981\u6b62\u8bbf\u95ee\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ee7\u627f\u987a\u5e8f<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b91\uff1a<\/strong>\u5982\u679c\u5b50\u7c7b\u7ee7\u627f\u4e86\u7236\u7c7b\uff0c\u90a3\u4e48\u5b50\u7c7b\u548c\u88ab\u7ee7\u627f\u7684\u7236\u7c7b\u8c01\u5148\u88ab\u5b9e\u4f8b\u5316\uff1f\u4e3a\u4ec0\u4e48\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ee7\u627f\u4e2d\uff0c\u5fc5\u987b\u5148\u5b9e\u4f8b\u5316\u7236\u7c7b\uff0c\u7136\u540e\u518d\u5b9e\u4f8b\u5316\u5b50\u7c7b\uff0c\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nclass father{\npublic:\n    father(){\n        cout&lt;&lt;\"father\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~father(){\n        cout&lt;&lt;\"father\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n};\nclass son : public father{\npublic:\n    son(){\n        cout&lt;&lt;\"son\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~son(){\n        cout&lt;&lt;\"son\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n};\nint main() {\n    son a;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>father\u88ab\u6784\u9020\nson\u88ab\u6784\u9020\nson\u88ab\u6790\u6784\nfather\u88ab\u6790\u6784<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ee3\u7801\u7ed3\u679c\u53ef\u4ee5\u5206\u6790\u51fa\uff0c\u7236\u7c7b\u5148\u88ab\u6784\u9020\uff0c\u7136\u540e\u518d\u6784\u9020\u5b50\u7c7b\u3002\u90a3\u4e48\u4e3a\u4ec0\u4e48\u4e0d\u80fd\u5148\u6784\u9020\u5b50\u7c7b\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u56e0\u4e3a\u5b50\u7c7b\u662f\u57fa\u4e8e\u7236\u7c7b\u8bde\u751f\u7684\uff0c\u5b50\u7c7b\u4e2d\u53ef\u80fd\u5b58\u5728\u9700\u8981\u57fa\u4e8e\u7236\u7c7b\u7684\u6210\u5458\u8fdb\u884c\u8ba1\u7b97\u7684\u60c5\u51b5\uff0c\u5982\u679c\u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b\u5b50\u7c7b\u5148\u5b9e\u4f8b\u5316\uff0c\u90a3\u4e48\u4f1a\u56e0\u4e3a\u5f53\u524d\u7236\u7c7b\u4e0d\u5b58\u5728\u5b9e\u4f53\u800c\u51fa\u73b0\u672a\u5b9a\u4e49\u9519\u8bef\u3002\u800c\u7236\u7c7b\u4e00\u5b9a\u4e0d\u4f1a\u57fa\u4e8e\u5b50\u7c7b\u6784\u9020\uff0c\u6240\u4ee5\u7edf\u4e00\u8bbe\u8ba1\u4e3a\u5148\u6784\u9020\u7236\u7c7b\u518d\u6784\u9020\u5b50\u7c7b\u7684\u65b9\u5f0f\u66f4\u52a0\u5408\u7406\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class father{\npublic:\n    int a;\n    father(){\n        a  = 10;\n    }\n};\n\nclass son : public father{\npublic:\n    int b;\n    son(){\n        b = a*a;\n    }\n};\nint main() {\n    son a;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\uff0c\u5b50\u7c7b\u7684\u53c2\u6570\u662f\u57fa\u4e8e\u7236\u7c7b\u7684\u53c2\u6570\u8fdb\u884c\u521d\u59cb\u5316\uff0c\u5982\u679c\u6b64\u65f6\u7236\u7c7b\u6ca1\u6709\u88ab\u6784\u9020\uff0c\u90a3\u4e48\u5b50\u7c7b\u521d\u59cb\u5316\u5c06\u4f1a\u51fa\u73b0\u95ee\u9898\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b92\uff1a<\/strong>\u6790\u6784\u51fd\u6570\u7684\u987a\u5e8f\u662f\u4ec0\u4e48\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u5176\u5b9e\u5b8c\u5168\u53ef\u4ee5\u81ea\u5df1\u63a8\u7406\u51fa\u6765\uff0c\u9996\u5148\u4ece\u51fd\u6570\u6808\u7684\u89d2\u5ea6\uff0c\u7236\u7c7b\u5148\u5b9e\u4f8b\u5316\u538b\u5165\u6808\u533a\uff0c\u7136\u540e\u5b50\u7c7b\u518d\u5b9e\u4f8b\u5316\u538b\u5165\u5c55\u533a\uff0c\u5728\u6808\u5f39\u51fa\u7684\u65f6\u5019\uff0c\u90a3\u4e48\u4e00\u5b9a\u662f\u5b50\u7c7b\u5148\u5f39\u51fa\uff0c\u518d\u7236\u7c7b\u5f39\u51fa\uff0c\u4e5f\u5c31\u662f\u5148\u8c03\u7528\u5b50\u7c7b\u518d\u8c03\u7528\u7236\u7c7b\u7684\u6790\u6784\u51fd\u6570\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u540c\u540d\u7ee7\u627f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7ee7\u627f\u7684\u8fc7\u7a0b\uff0c\u5f80\u5f80\u4f1a\u9047\u5230\u4e00\u79cd\u60c5\u51b5\u2014\u2014\u7236\u7c7b\u7684\u51fd\u6570\u5e76\u4e0d\u662f\u6211\u60f3\u8981\u7684\uff0c\u4f46\u662f\u6211\u7684\u51fd\u6570\u4f5c\u7528\u548c\u8fd9\u4e2a\u51fd\u6570\u4e00\u81f4\uff0c\u867d\u7136\u5199\u4e00\u4e2a\u65b0\u51fd\u6570\u662f\u89e3\u51b3\u65b9\u6cd5\u7684\u4e00\u79cd\uff0c\u4f46\u662f\u8fd9\u4f1a\u5bfc\u81f4\u540c\u4f5c\u7528\u7684\u51fd\u6570\u4e0d\u65ad\u589e\u591a\uff0c\u663e\u7136\u8fdd\u80cc\u4e86\u7ee7\u627f\u7684\u6700\u521d\u89c2\u5ff5\uff0c\u4e8e\u662fc++\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bed\u6cd5\u53ef\u4ee5\u652f\u6301\u5728\u5b50\u7c7b\u4ee5\u540c\u540d\u51fd\u6570\u91cd\u5199\u7236\u7c7b\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\nclass father{\npublic:\n    father(){\n        cout&lt;&lt;\"father\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~father(){\n        cout&lt;&lt;\"father\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n    void func(){\n        cout&lt;&lt;\"\u6211\u662ffather\"&lt;&lt;endl;\n    }\n};\nclass son : public father{\npublic:\n    son(){\n        cout&lt;&lt;\"son\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~son(){\n        cout&lt;&lt;\"son\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n    void func(){\n        cout&lt;&lt;\"\u6211\u662fson\"&lt;&lt;endl;\n    }\n};\nint main() {\n    son a;\n    a.func();\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>father\u88ab\u6784\u9020\nson\u88ab\u6784\u9020\n\u6211\u662fson\nson\u88ab\u6790\u6784\nfather\u88ab\u6790\u6784<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4ee3\u7801\u7684\u6267\u884c\u7ed3\u679c\u4e2d\uff0c\u53ef\u4ee5\u770b\u5230\u51fd\u6570\u8f93\u51fa\u7684\u662f\u5b50\u7c7b\u91cd\u5199\u7684func\u51fd\u6570\u3002\u867d\u7136\u7236\u7c7b\u88ab\u5b50\u7c7b\u91cd\u5199\uff0c\u4f46\u662f\u7236\u7c7b\u7684\u540c\u540d\u51fd\u6570\u5e76\u975e\u5c31\u65e0\u6cd5\u8bbf\u95ee\u4e86\uff0c\u4f60\u53ef\u4ee5\u901a\u8fc7\u58f0\u660e\u7684\u65b9\u5f0f\u4f7f\u7528func\u3002\u6bd4\u5982<code>father::func()<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7528\u4e0a\u9762\u7684\u4ee3\u7801\u4e3e\u4f8b\u3002\u5728main\u51fd\u6570\u4e2d\u9700\u8981\u8fd9\u6837\u663e\u5f0f\u58f0\u660e\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>int main() {\n    son a;\n    a.func();\/\/\u8c03\u7528\u5b50\u7c7b\u91cd\u5199\u7684func\n    a.father::func();\/\/\u901a\u8fc7\u4f5c\u7528\u4e8e\u663e\u5f0f\u58f0\u660e\u8c03\u7528father\u7c7b\u4e2d\u7684func\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6709\u4e00\u4e2a\u70b9\u9700\u8981\u8865\u5145\uff1a\u5b50\u7c7b\u7ee7\u627f\u540e\uff0c\u5c31\u5e94\u8be5\u4ee5\u5b50\u7c7b\u4e3a\u4e3b\u4f53\uff0c\u5c31\u50cf\u5b69\u5b50\u957f\u5927\u4e86\u8981\u79bb\u5f00\u7236\u6bcd\uff0c\u8fd9\u4e2a\u65f6\u5019\u5b69\u5b50\u5c31\u662f\u4e00\u4e2a\u72ec\u7acb\u7684\u4e2a\u4f53\u4e86\u3002\u6709\u4e86\u8fd9\u79cd\u601d\u60f3\uff0c\u5c31\u8fd8\u6709\u4e00\u4e2a\u70b9\u5bb9\u6613\u7406\u89e3\u4e86\uff0c\u5f53\u7236\u7c7b\u51fa\u73b0\u4e86\u51fd\u6570\u91cd\u8f7d\u65f6\uff0c\u5982\u679c\u5b50\u7c7b\u91cd\u5199\u4e86\u5176\u4e2d\u4e00\u4e2a\u51fd\u6570\uff0c\u90a3\u4e48\u7236\u7c7b\u7684\u6240\u6709\u540c\u540d\u51fd\u6570\u5168\u90e8\u90fd\u4f1a\u88ab\u9690\u85cf\uff0c\u9664\u975e\u88ab\u7c7b\u540d\u663e\u5f0f\u8c03\u7528\u3002\u4e5f\u5c31\u662f\u5b69\u5b50\u72ec\u7acb\u4e86\uff0c\u6709\u4e86\u81ea\u5df1\u7684\u5bb6\u5ead\uff0c\u90a3\u4e48\u6838\u5fc3\u5c31\u8981\u653e\u5728\u81ea\u5df1\u7684\u5bb6\u5ead\u4e2d\u4e86\uff0c\u6bd4\u5982\u5e73\u65f6\u4e00\u65e5\u4e09\u9910\u7684\u4e70\u83dc\uff0c\u5728\u4e0d\u5f3a\u8c03\u7684\u60c5\u51b5\u4e0b\uff0c\u5f80\u5f80\u662f\u7ed9\u81ea\u5df1\u5bb6\u91cc\u9762\u4e70\u83dc\uff0c\u800c\u4e0d\u662f\u4e70\u83dc\u540e\u5343\u91cc\u8fe2\u8fe2\u9001\u5230\u7236\u6bcd\u5bb6\u4e2d\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#7bdcb5\" class=\"has-inline-color\">\u9664\u4e86\u540c\u540d\u51fd\u6570\u7684\u7ee7\u627f\uff0c\u540c\u6837\u7684\u4e5f\u53ef\u4ee5\u8fdb\u884c\u540c\u540d\u53d8\u91cf\u7684\u7ee7\u627f\uff0c\u7528\u6cd5\u4e0e\u540c\u540d\u51fd\u6570\u5b8c\u5168\u4e00\u81f4\u3002\u800c\u5bf9\u4e8e\u7279\u6b8a\u7684\u9759\u6001\u6210\u5458\u53d8\u91cf\u548c\u9759\u6001\u6210\u5458\u51fd\u6570\uff0c\u548c\u666e\u901a\u7684\u53d8\u91cf\u4e0e\u51fd\u6570\u5904\u7406\u65b9\u6cd5\u4e5f\u662f\u5b8c\u5168\u4e00\u81f4\u7684\u3002<\/mark><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u591a\u91cd\u7ee7\u627f<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">\u591a\u91cd\u57fa\u7840\u7684\u65b9\u6cd5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u91cd\u7ee7\u627f\u4e0e\u666e\u901a\u7ee7\u627f\u6ca1\u6709\u4ec0\u4e48\u533a\u522b\uff0c\u4e0d\u8fc7\u6bcf\u4e00\u4e2a\u88ab\u7ee7\u627f\u7684\u5bf9\u8c61\u53ef\u4ee5\u5355\u72ec\u58f0\u660e\u7ee7\u627f\u6743\u9650\uff0c\u591a\u4e2a\u7236\u7c7b\u4e4b\u95f4\u7528<code>\uff0c<\/code>\u53f7\u94fe\u63a5\uff0c\u5e76\u4e14\u6784\u9020\u987a\u5e8f\u4ece\u5de6\u5230\u53f3\u6700\u540e\u5b50\u7c7b\u8fdb\u884c\u6784\u9020\uff0c\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass mother{\npublic:\n    mother(){\n        cout&lt;&lt;\"mother\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~mother(){\n        cout&lt;&lt;\"mother\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n    void func(){\n        cout&lt;&lt;\"\u6211\u662fmother\"&lt;&lt;endl;\n    }\n};\n\nclass father{\npublic:\n    father(){\n        cout&lt;&lt;\"father\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~father(){\n        cout&lt;&lt;\"father\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n    void func(){\n        cout&lt;&lt;\"\u6211\u662ffather\"&lt;&lt;endl;\n    }\n};\n\nclass son : public father ,public mother{\npublic:\n    son(){\n        cout&lt;&lt;\"son\u88ab\u6784\u9020\"&lt;&lt;endl;\n    }\n    ~son(){\n        cout&lt;&lt;\"son\u88ab\u6790\u6784\"&lt;&lt;endl;\n    }\n    void func(){\n        cout&lt;&lt;\"\u6211\u662fson\"&lt;&lt;endl;\n    }\n};\n\n\nint main() {\n    son a;\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>father\u88ab\u6784\u9020\nmother\u88ab\u6784\u9020\nson\u88ab\u6784\u9020\nson\u88ab\u6790\u6784\nmother\u88ab\u6790\u6784\nfather\u88ab\u6790\u6784<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u540c\u540d\u51b2\u7a81<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u7236\u7c7b\u4e4b\u95f4\u5982\u679c\u7ed9\u81ea\u7c7b\u4e2d\u4e0d\u5b58\u5728\u540c\u540d\u51fd\u6570\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u50cf\u5355\u7ee7\u627f\u4e00\u6837\u64cd\u4f5c\u3002\u4f46\u662f\u5982\u679c\u5b58\u5728\u540c\u540d\u51fd\u6570\uff0c\u6bd4\u5982\u4f8b\u5b50\u4e2d\u7684<code>func<\/code>\uff0c\u6b64\u65f6\u5c31\u9700\u8981\u7279\u522b\u6307\u660e\u662f\u54ea\u4e00\u4e2a\u7c7b\u4e2d\u7ee7\u627f\u4e0b\u6765\u7684\u6210\u5458\u3002\u7531\u4e8e\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u901a\u5e38\u662f\u591a\u4e2a\u4eba\u5408\u4f5c\uff0c\u60f3<code>size<\/code>\u8fd9\u79cd\u9ad8\u9891\u51fd\u6570\u5c31\u4f1a\u5bfc\u81f4\u51b2\u7a81\uff0c\u6240\u4ee5\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\u4e00\u822c\u907f\u514d\u4f7f\u7528\u591a\u7ee7\u627f\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u83f1\u5f62\u7ee7\u627f<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u57fa\u672c\u7c7b<code>Animal<\/code>\uff0c\u5b83\u6709\u4e00\u4e2a\u6210\u5458\u51fd\u6570<code>makeSound()<\/code>\u3002\u7136\u540e\u6211\u4eec\u6709\u4e24\u4e2a\u6d3e\u751f\u7c7b<code>Mammal<\/code>\u548c<code>Bird<\/code>\uff0c\u5b83\u4eec\u90fd\u7ee7\u627f\u81ea<code>Animal<\/code>\u3002\u6700\u540e\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u7c7b<code>Bat<\/code>\uff0c\u5b83\u540c\u65f6\u7ee7\u627f\u81ea<code>Mammal<\/code>\u548c<code>Bird<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>Bat<\/code>\u7c7b\u7ee7\u627f\u4e86\u4e24\u4e2a<code>makeSound()<\/code>\u51fd\u6570\uff0c\u4e00\u4e2a\u6765\u81ea<code>Mammal<\/code>\uff0c\u4e00\u4e2a\u6765\u81ea<code>Bird<\/code>\u3002\u8fd9\u5c31\u5f62\u6210\u4e86\u4e00\u4e2a\u83f1\u5f62\u7ed3\u6784\uff0c\u56e0\u4e3a<code>Bat<\/code>\u7c7b\u901a\u8fc7\u4e24\u4e2a\u4e0d\u540c\u7684\u8def\u5f84\u7ee7\u627f\u4e86<code>Animal<\/code>\u7c7b\u3002\u8fd9\u79cd\u7ed3\u6784\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e8c\u4e49\u6027\u95ee\u9898\uff0c\u56e0\u4e3a\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u5e94\u8be5\u8c03\u7528\u54ea\u4e2a<code>makeSound()<\/code>\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f62\u8c61\u7684\u8bb2\uff0c\u5c31\u662f\u4e00\u4e2a\u51fd\u6570\u88ab\u4e24\u4e2a\u7c7b\u7ee7\u627f\u53d8\u6210\u4e86\u4e24\u4e2a\u4e00\u6a21\u4e00\u6837\u7684\u51fd\u6570\uff0c\u800c\u4e24\u4e2a\u7c7b\u88ab\u540c\u4e00\u4e2a\u5b50\u7c7b\u7ee7\u627f\uff0c\u90a3\u4e48\u8fd9\u4e2a\u5b50\u7c7b\u5c31\u5f04\u4e0d\u660e\u767d\u4e86\uff0c\u56e0\u4e3a\u6709\u4e24\u4e2a\u4e00\u6a21\u4e00\u6837\u7684\u51fd\u6570\u7f16\u8bd1\u5668\u4e0d\u77e5\u9053\u8be5\u600e\u4e48\u505a\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u60c5\u51b5\u5f88\u50cf\u4e4b\u524d\u63d0\u5230\u7684\u591a\u91cd\u7ee7\u627f\u5f15\u53d1\u7684\u4e8c\u4e49\u6027\uff0c\u4e0d\u8fc7\u8fd9\u79cd\u60c5\u51b5\u66f4\u7279\u6b8a\uff0c\u56e0\u4e3a\u5b83\u867d\u7136\u5728\u6700\u540e\u4e00\u5c42\u7684\u7ee7\u627f\u4e2d<code>makeSound()<\/code>\u51fa\u73b0\u4e86\u4e8c\u4e49\u6027\uff0c\u4f46\u662f\u5b9e\u9645\u4e0a<code>makeSound()<\/code>\u90fd\u6765\u81ea\u4e8e\u7c7b<code>Animal<\/code>\uff0c\u4e5f\u5c31\u662f\u672c\u8d28\u4e0a\u8fd9\u4e2a\u51fd\u6570\u5c31\u662f\u540c\u4e00\u4e2a\uff0c\u89e3\u51b3\u65b9\u6cd5\u4e5f\u5f88\u7b80\u5355\uff0c\u90a3\u5c31\u662f\u8ba9\u7f16\u8bd1\u5668\u77e5\u9053\u8fd9\u4e24\u4e2a\u51fd\u6570\u5176\u5b9e\u5c31\u662f\u540c\u4e00\u4e2a\u51fd\u6570\u5c31\u53ef\u4ee5\u4e86\uff0c\u4e8e\u662f\u7531\u6b64\u8bde\u751f\u4e86\u4e00\u79cd\u65b9\u5f0f\u2014\u2014\u865a\u7ee7\u627f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u865a\u7ee7\u627f\u7684\u6982\u5ff5<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u865a\u7ee7\u627f<\/strong>\uff08<code>virtual inheritance<\/code>\uff09\u662fC++\u4e2d\u7684\u4e00\u79cd\u7ee7\u627f\u673a\u5236\uff0c\u7528\u4e8e\u89e3\u51b3\u591a\u91cd\u7ee7\u627f\u4e2d\u7684\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u3002\u83f1\u5f62\u7ee7\u627f\u95ee\u9898\u4f1a\u5bfc\u81f4\u57fa\u7c7b\u7684\u6210\u5458\u5728\u6d3e\u751f\u7c7b\u4e2d\u88ab\u591a\u6b21\u7ee7\u627f\uff0c\u4ece\u800c\u5f15\u53d1\u6570\u636e\u5197\u4f59\u548c\u4e0d\u4e00\u81f4\u6027\uff0c\u5176\u4e3b\u8981\u7279\u70b9\u4e3a\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u907f\u514d\u91cd\u590d\u7ee7\u627f<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u901a\u8fc7\u865a\u7ee7\u627f\uff0c\u865a\u57fa\u7c7b\u5728\u6700\u6d3e\u751f\u7c7b\u4e2d\u53ea\u4f1a\u88ab\u521d\u59cb\u5316\u4e00\u6b21\uff0c\u800c\u4e0d\u662f\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u521d\u59cb\u5316\u4e00\u6b21\u3002\u8fd9\u907f\u514d\u4e86\u57fa\u7c7b\u6210\u5458\u7684\u91cd\u590d\u7ee7\u627f\u548c\u521d\u59cb\u5316\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u865a\u57fa\u8868\u6307\u9488<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u5728\u865a\u7ee7\u627f\u4e2d\uff0c\u6bcf\u4e2a\u6d3e\u751f\u7c7b\u90fd\u4f1a\u6709\u4e00\u4e2a\u6307\u5411\u865a\u57fa\u7c7b\u7684\u865a\u57fa\u8868\u6307\u9488\uff08<code>vptr<\/code>\uff09\u3002\u8fd9\u4e9b\u6307\u9488\u5728\u4e0d\u540c\u7684\u6d3e\u751f\u7c7b\u4e2d\u662f\u4e0d\u540c\u7684\uff0c\u4f46\u5b83\u4eec\u6700\u7ec8\u6307\u5411\u540c\u4e00\u4e2a\u865a\u57fa\u7c7b\u5b9e\u4f8b\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u6700\u6d3e\u751f\u7c7b\u8d1f\u8d23\u521d\u59cb\u5316<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u865a\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u53ea\u80fd\u7531\u6700\u6d3e\u751f\u7c7b\u8d1f\u8d23\u8c03\u7528\u3002\u5373\u4f7f\u4e2d\u95f4\u7684\u6d3e\u751f\u7c7b\uff08\u5982<code>B<\/code>\u548c<code>C<\/code>\uff09\u7ee7\u627f\u4e86\u865a\u57fa\u7c7b\uff0c\u5b83\u4eec\u4e5f\u4e0d\u4f1a\u8c03\u7528\u865a\u57fa\u7c7b\u7684\u6784\u9020\u51fd\u6570\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<p class=\"wp-block-paragraph\">\u865a\u57fa\u7c7b\u662f\u6307\u88ab\u865a\u7ee7\u627f\u7684\u57fa\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class \u7c7b\u540d : virtual \u7ee7\u627f\u6743\u9650 \u7236\u7c7b\u7c7b\u540d<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u4e3e\u4f8b<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class A {\npublic:\n    int value;\n    A(int v) : value(v) {}\n};\nclass B : virtual public A { \/\/\u865a\u57fa\u7c7b\npublic:\n    B() : A(0) {} \n};<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u865a\u7ee7\u627f\u89e3\u6790<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ece\u53d1\u73b0\u95ee\u9898\u5230\u89e3\u51b3\u95ee\u9898\u7684\u601d\u8def\u6765\u8bb2\u89e3\uff0c\u9996\u5148\u5148\u629b\u51fa\u4e00\u4e2a\u95ee\u9898\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass A {\npublic:\n    int value;\n    A(int v) : value(v) {}\n};\nclass B : public A {\npublic:\n    B() : A(10) {} \n};\nclass C : public A {\npublic:\n    C() : A(0) {} \n};\nclass D : public B, public C {\npublic:\n   \n};\nint main() {\n    D obj;\n    std::cout &lt;&lt; obj.value &lt;&lt; std::endl; \/\/ \u8f93\u51fa10\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6682\u65f6\u5ffd\u7565\u5bf9<code>D\u7c7b<\/code>\u7684\u5b9e\u73b0\uff0c\u5148\u8003\u8651<code>D\u7c7b<\/code>\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u60c5\u51b5\uff0c<code>D\u7c7b<\/code>\u5728\u5b9e\u4f8b\u5316\u7684\u8fc7\u7a0b\u4e2d\uff0c\u53d1\u73b0\u4e86\u6765\u81ea<code>B\u7c7b<\/code>\u7684<code>value<\/code>\u548c\u6765\u81ea<code>C\u7c7b<\/code>\u7684<code>value<\/code>\uff0c\u5f53\u5b83\u9700\u8981\u88ab\u8c03\u7528<code>value<\/code>\u65f6\uff0c\u4f1a\u5bfc\u81f4\u7cfb\u7edf\u4e0d\u77e5\u9053\u8c03\u7528\u54ea\u4e00\u4e2a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u4ed4\u7ec6\u7684\u89c2\u5bdf\uff0c\u5b9e\u9645\u4e0a\u5f88\u5bb9\u6613\u53d1\u73b0\uff0c\u8fd9\u4e24\u4e2a<code>value<\/code>\u662f\u540c\u4e00\u4e2a\u3002\u4f46\u662f\u88ab\u67d0\u79cd\u795e\u79d8\u7684\u529b\u91cf\u5206\u5316\u6210\u4e86\u4e24\u4e2a\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u518d\u6b21\u501f\u52a9\u4ec0\u4e48\u7684\u529b\u91cf\u5c06\u4e24\u4e2a<code>value<\/code>\u5408\u5e76\u6210\u4e3a\u4e00\u4e2a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5148\u770b\u4e00\u4e0b\u6b63\u786e\u7684\u65b9\u6cd5\uff0c\u540e\u7eed\u4f1a\u597d\u7406\u89e3\u4e00\u70b9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream&gt;\nusing namespace std;\n\nclass A {\npublic:\n    int value;\n    A(int v) : value(v) {}\n};\n\nclass B : virtual public A {\npublic:\n    B() : A(10) {} \n};\n\nclass C : virtual public A {\npublic:\n    C() : A(0) {}\n};\n\nclass D : public B, public C {\npublic:\n    D() : A(10), B(), C() {}\n};\n\nint main() {\n    D obj;\n    std::cout &lt;&lt; obj.value &lt;&lt; std::endl; \/\/ \u8f93\u51fa10\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6682\u65f6\u5ffd\u7565\u8fd9\u4e2a\u6b63\u786e\u7684\u4ee3\u7801\uff0c\u8003\u8651\u5982\u4f55\u89e3\u51b3\uff0c\u6700\u76f4\u63a5\u7684\u4e00\u79cd\u89e3\u51b3\u65b9\u6848\uff0c\u90a3\u5c31\u662f\u53ea\u751f\u6210\u4e00\u4e2a<code>value<\/code>\uff0c\u8ba9<code>B\u7c7b<\/code>\u548c<code>C\u7c7b<\/code>\u4e2d\u5206\u522b\u5b58\u50a8\u4e00\u4e2a\u6307\u9488\uff0c\u6307\u5411\u8fd9\u4e2a<code>value<\/code>\u3002\u8fd9\u6837\u5c31\u4f1a\u53d1\u73b0<code>B\u7c7b<\/code>\u548c<code>C\u7c7b<\/code>\uff0c\u5c31\u4f1a\u5171\u4eab\u540c\u4e00\u4e2a<code>value<\/code>\u7a7a\u95f4\u3002\u5f53\u7136\u4e2d\u95f4\u7684\u8fc7\u7a0b\u80af\u5b9a\u4e0d\u662f\u8fd9\u4e48\u7b80\u5355\uff0c\u6211\u4eec\u6682\u65f6\u5c06\u8fd9\u4e2a\u64cd\u4f5c\u5047\u5b9a\u4e3a\u201c<code>\u795e\u79d8\u9ed1\u76d2\u64cd\u4f5c<\/code>\u201d\uff0c\u8fd9\u4e2a\u9ed1\u76d2\u64cd\u4f5c\u5b9a\u4e49\u5982\u4e0b\uff1a\u5728\u53d1\u751f\u865a\u7ee7\u627f\u7684\u65f6\u5019\uff0c\u5b50\u7c7b\u4f1a\u83b7\u5f97\u4e00\u4e2a<code>\u9ed1\u76d2\u7279\u5f81\u7801<\/code>\uff0c\u5c06<code>\u9ed1\u76d2\u7279\u5f81\u7801<\/code>\u8fdb\u884c<code>\u795e\u79d8\u9ed1\u76d2\u64cd\u4f5c<\/code>\uff0c\u53ef\u4ee5\u83b7\u5f97\u627e\u5230<code>value<\/code>\u6240\u5728\u7684\u7a7a\u95f4\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b91\uff1a<\/strong>\u5728\u4f8b\u5b50\u4e2d\u7684D\u7c7b\u5982\u679c\u662f\u8fdb\u884c\u7ee7\u627fB\u7c7b\u548cC\u7c7b\uff0c\u90a3\u4e48\u5728\u5b9e\u4f8b\u5316\u7684\u65f6\u5019\uff0c\u4f1a\u4ece\u8001\u8001\u8001&#8230;\u7956\u5b97\u90a3\u91cc\u5f00\u59cb\u6784\u9020\uff0c\u90a3\u4e48\u5728B\u7c7b\u548cC\u7c7b\u53d1\u751f\u5bf9A\u7684\u6784\u9020\u65f6\u5c31\u4f1a\u51fa\u73b0\u91cd\u590d\uff0c\u5e94\u8be5\u5982\u4f55\u89e3\u51b3\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u6838\u5fc3\u70b9\u7684\u95ee\u9898\u5f88\u5f88\u91cd\u8981\uff0c\u5982\u679c\u5d4c\u5957\u7684\u5c42\u6570\u8d8a\u591a\uff0c\u90a3\u4e48\u5c31\u4f1a\u5bfc\u81f4\u591a\u6b21\u53cd\u590d\u8c03\u7528\u6784\u9020\uff0c\u8fd9\u663e\u7136\u662f\u4e0d\u53ef\u53d6\u7684\uff0c\u6240\u4ee5\u89e3\u51b3\u65b9\u6848\u5c31\u662f\u4f7f\u7528<strong>\u6700\u7ec8\u6d3e\u751f\u7c7b\u8d1f\u8d23\u521d\u59cb\u5316<\/strong>\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u4e0d\u7ba1\u53d1\u751f\u4e86\u591a\u5c11\u5c42\u7ee7\u627f\uff0c\u53ea\u8981\u4f60\u4e0d\u662f\u6700\u540e\u4e00\u5c42\uff0c\u90a3\u4e48\u4f60\u5bf9A\u8c03\u7528\u6784\u9020\u7684\u6743\u529b\u5c31\u88ab\u5265\u593a\u4e86\uff0c\u53ea\u5141\u8bb8\u6700\u540e\u4e00\u5c42\u5bf9\u8001\u7956\u5b97\u8fdb\u884c\u6784\u9020\u51fd\u6570\u7684\u8c03\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u6ce8\u610f\uff0c\u8fd9\u5e76\u4e0d\u5f71\u54cd\u4e2d\u95f4\u5c42\u5199\u8c03\u7528\u8001\u7956\u5b97\u6784\u9020\u51fd\u6570\u7684\u4ee3\u7801\uff0c\u4f60\u5199\u5f52\u4f60\u5199\uff0c\u81f3\u4e8e\u7528\u4e0d\u7528\u5c31\u5f97\u7f16\u8bd1\u5668\u8bf4\u4e86\u7b97\uff0c\u5f53\u7136\u4e00\u822c\u4e0d\u4f1a\u7528\uff0c\u9664\u975e\u4f60\u662f\u8001\u7956\u5b97\u6700\u75bc\u7231\u7684\u5b59\u5b50\uff08\u6700\u540e\u4e00\u5c42\uff09\uff0c\u8fd9\u4e5f\u5c31\u907f\u514d\u4e86\u591a\u6b21\u8c03\u7528\u540c\u4e00\u4e2a\u6784\u9020\u51fd\u6570\u7684\u60c5\u51b5\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class D : public B, public C {\npublic:\n    D() : A(10), B(), C() {}\/\/\u7531D\u7c7b\u8d1f\u8d23\u5bf9A\u7c7b\u7684\u6784\u9020\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b92<\/strong>\uff1a\u5728<code>D\u7c7b<\/code>\u5b9e\u4f8b\u5316\u540e\uff0c\u5c31\u6709\u4e24\u4e2a<code>\u9ed1\u76d2\u7279\u5f81\u7801<\/code>\u4e86\uff0c\u90a3\u4e48\u5982\u679c\u8981\u8c03\u7528<code>value<\/code>\uff0c\u4f7f\u7528\u8c01\u7684<code>\u9ed1\u76d2\u7279\u5f81\u7801<\/code>\u8fdb\u884c<code>\u795e\u79d8\u9ed1\u76d2\u64cd\u4f5c<\/code>\u83b7\u53d6\u5230<code>value<\/code>\u5462\uff1f<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u4e2a\u662f\u7f16\u8bd1\u914d\u7f6e\u7684\uff0c\u7f16\u8bd1\u5668\u4f1a\u6839\u636e\u67d0\u79cd\u89c4\u5219\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u6700\u9ad8\u6548\u7684\u65b9\u5f0f\u6765\u83b7\u53d6<code>value<\/code>\uff0c\u56e0\u4e3a\u5b9e\u9645\u4e0a\u4e0d\u7ba1\u54ea\u4e2a\u7279\u5f81\u7801\u6700\u7ec8\u6307\u5411\u7684\u662f\u540c\u4e00\u4e2a<code>value<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8bf4\u5b8c\u4e86\u5927\u4f53\u6846\u67b6\uff0c\u63a5\u4e0b\u6765\u5c31\u5e94\u8be5\u89e3\u5f00\u9ed1\u76d2\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e4b\u524d\u7b80\u5355\u6982\u62ec\u4e3a\u865a\u7ee7\u627f\u7684\u7c7b\u4f1a\u4ea7\u751f\u4e00\u4e2a\u6307\u9488\u6307\u5411<code>value<\/code>\uff0c\u8fd9\u91cc\u662f\u4e00\u79cd\u6781\u7b80\u7684\u601d\u7ef4\u7684\u7c7b\u6bd4\uff0c\u5728\u5b9e\u9645\u4e0a\uff0c\u865a\u57fa\u7840\u5e76\u4e0d\u76f4\u63a5\u751f\u6210\u4e00\u4e2a\u6307\u5411<code>value<\/code>\u7684\u6307\u9488\uff0c\u800c\u662f\u4f1a\u5728\u5185\u5b58\u7684\u5168\u5c40\u533a\u751f\u6210\u4e00\u4e2a\u865a\u57fa\u8868\uff0c\u800c\u865a\u7ee7\u627f\u751f\u6210\u7684\u6307\u9488\u662f\u6307\u5411\u8fd9\u4e2a\u865a\u57fa\u8868\u7684\u6307\u9488\uff0c\u4e5f\u79f0\u4e3a\u865a\u57fa\u8868\u6307\u9488\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u5c31\u4f1a\u6709\u4eba\u601d\u8003\uff0c\u865a\u57fa\u8868\u662f\u4ec0\u4e48\uff1f\u4e3a\u4ec0\u4e48\u8981\u4e00\u4e2a\u6307\u9488\u6307\u5411\u865a\u57fa\u8868\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u865a\u57fa\u8868\u672c\u8d28\u4e0a\u8bb2\u5c31\u50cf\u662f\u4e00\u4e2a\u6570\u7ec4\uff0c\u8fd9\u4e2a\u6570\u7ec4\u5b58\u653e\u7684\u662f\u504f\u79fb\u91cf\u3002\u5b83\u7684\u4f5c\u7528\u5c31\u50cf\u662f\u4e00\u4e2a\u76ee\u5f55\uff0c\u6bd4\u5982\u6211\u751f\u6210\u7684\u5bf9\u8c61\u57fa\u5730\u5740\u662f<code>0x00<\/code>\uff0c<code>value<\/code>\u7684\u503c\u662f<code>0x10<\/code>\uff0c\u90a3\u4e48\u8fd9\u4e2a\u865a\u57fa\u8868\u4e2d\u5b58\u653e\u7684\u5c31\u662f\u504f\u79fb\u91cf<code>10<\/code>\uff0c\u5f53\u5e0c\u671b\u8bbf\u95ee\u5230<code>value<\/code>\u7684\u65f6\u5019\uff0c\u53ea\u9700\u8981\u901a\u8fc7\u865a\u57fa\u8868\u6307\u9488\u8bbf\u95ee\u865a\u57fa\u8868\uff0c\u6839\u636e\u60f3\u8bbf\u95ee\u7684\u6210\u5458\u4ece\u865a\u57fa\u8868\u4e2d\u67e5\u8be2\u5230\u8fd9\u4e2a\u6210\u5458\u5bf9\u5e94\u7684\u504f\u79fb\u91cf\u662f<code>10<\/code> \uff0c\u7136\u540e\u7528<code>10<\/code>\u52a0\u4e0a\u8fd9\u4e2a\u5bf9\u8c61\u7684\u57fa\u5730\u5740\uff0c\u5c31\u53ef\u4ee5\u83b7\u53d6\u5230<code>value<\/code>\u4e86\u3002\uff08\u8865\u5145\uff0c\u8fd9\u662f\u4e00\u79cd\u8bbe\u8ba1\u65b9\u5f0f\uff0c\u4f46\u662f\u6211\u5e76\u6ca1\u6709\u627e\u5230\u8d44\u6599\u660e\u786e\u7684\u8868\u793a\u57fa\u5740\u662f\u6700\u6d3e\u751f\u7c7b\u7684\u5bf9\u8c61\u7684\u9996\u5730\u5740\u3002\u6240\u4ee5\u6700\u540e\u7684\u7ed3\u8bba\u662f\uff1a\u504f\u79fb\u91cf\u4e0d\u4e00\u5b9a\u662f\u4ee5\u5bf9\u8c61\u7684\u9996\u5730\u5740\u4e3a\u57fa\u5740\u7684\uff0c\u8fd9\u4e2a\u53d6\u51b3\u4e8e\u7f16\u8bd1\u5668\u3002\uff09<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u91cc\u5c31\u80fd\u4f53\u4f1a\u51fa\u4e86\uff0c\u5728\u4e4b\u524d\u7684\u5206\u6790\u4e2d\uff0c\u6240\u8c13\u7684<code>\u9ed1\u76d2\u7279\u5f81\u7801<\/code>\u5c31\u662f\u865a\u57fa\u8868\u6307\u9488\uff0c\u800c<code>\u795e\u79d8\u9ed1\u76d2\u64cd\u4f5c<\/code>\u4fbf\u662f\u901a\u8fc7\u865a\u57fa\u8868\u6307\u9488\u67e5\u8be2\u504f\u79fb\u91cf\uff0c\u518d\u6839\u636e\u504f\u79fb\u91cf\u83b7\u53d6\u5230<code>value<\/code>\u7684\u8fc7\u7a0b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u7531\u4e8e\u83f1\u5f62\u7ee7\u627f\u7684\u65f6\u5019\uff0c\u6700\u7ec8\u6d3e\u751f\u7c7b\u4f1a\u7ee7\u627f\u591a\u4e2a\u7236\u7c7b\u3002\u6240\u4ee5\u6bcf\u4e2a\u7236\u7c7b\u7684\u865a\u57fa\u8868\u548c\u865a\u57fa\u8868\u6307\u9488\u90fd\u662f\u5404\u4e0d\u76f8\u540c\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b93\uff1a<\/strong>\u5982\u679c\u5728\u591a\u91cd\u7ee7\u627f\u53d1\u751f\u540e\uff0c\u6700\u7ec8\u6d3e\u751f\u7c7b\u7684\u5185\u5b58\u5e03\u5c40\u662f\u600e\u4e48\u6837\u7684\uff1f<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='http:\/\/blog.chxzking.cn\/wp-content\/uploads\/2024\/08\/740M0NV8FX1_ZZLGDA-1024x610.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"610\" data-original=\"http:\/\/blog.chxzking.cn\/wp-content\/uploads\/2024\/08\/740M0NV8FX1_ZZLGDA-1024x610.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-1165\"  sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/div><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5185\u5b58\u5e03\u5c40\u4e2d\u6709\u51e0\u4e2a\u6bd4\u8f83\u91cd\u8981\u7684\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7236\u7c7b\u7684\u865a\u57fa\u8868\u6307\u9488\u88ab\u7f16\u8bd1\u751f\u6210\uff0c\u800c\u4e14\u5b58\u653e\u5728\u7c7b\u7684\u9996\u4e2a\u6210\u5458\u7684\u4e4b\u524d\u3002<\/li>\n\n\n\n<li>\u6700\u7ec8\u6d3e\u751f\u7c7b\u7684\u7236\u7c7b\u5b9e\u4f8b\u4e2d\u4e0d\u5b58\u5728\u88ab\u865a\u7ee7\u627f\u7684\u6210\u5458\uff0c\u8fd9\u4e2a\u6210\u5458\u88ab\u5b58\u653e\u5728\u6700\u7ec8\u6d3e\u751f\u7c7b\u7684\u6700\u5e95\u90e8\u3002<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><kbd>\u591a\u6001<\/kbd><\/h1>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u6001\uff08Polymorphism\uff09\u662f\u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u4e2d\u7684\u4e00\u4e2a\u6838\u5fc3\u6982\u5ff5\uff0c\u6307\u7684\u662f\u540c\u4e00\u4e2a\u63a5\u53e3\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u5b9e\u73b0\u65b9\u5f0f\u3002\u591a\u6001\u6027\u4f7f\u5f97\u7a0b\u5e8f\u53ef\u4ee5\u5728\u8fd0\u884c\u65f6\u51b3\u5b9a\u8c03\u7528\u54ea\u4e2a\u5177\u4f53\u7684\u5b9e\u73b0\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u591a\u6001\u7684\u4e3b\u8981\u573a\u666f\u662f\u8bbe\u8ba1\u516c\u7528\u63a5\u53e3\uff0c\u5b83\u53ef\u4ee5\u901a\u8fc7\u4e00\u4e2a\u7edf\u4e00\u7684\u63a5\u53e3\u5728\u4e0d\u540c\u7684\u60c5\u51b5\u4e0b\uff0c\u8bbf\u95ee\u4e0d\u540c\u7684\u6d3e\u751f\u7c7b\u4e2d\u7684\u91cd\u5199\u7684\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u591a\u6001\u6027\u901a\u5e38\u901a\u8fc7\u62bd\u8c61\u7c7b\u548c\u7eaf\u865a\u51fd\u6570\u6765\u5b9e\u73b0\u3002\u62bd\u8c61\u7c7b\u4e0d\u80fd\u5b9e\u4f8b\u5316\uff0c\u53ea\u80fd\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\uff0c\u800c\u7eaf\u865a\u51fd\u6570\u5219\u5fc5\u987b\u5728\u6d3e\u751f\u7c7b\u4e2d\u5b9e\u73b0\u3002<\/li>\n\n\n\n<li>\u867d\u7136\u591a\u6001\u6027\u63d0\u4f9b\u4e86\u5f88\u5927\u7684\u7075\u6d3b\u6027\uff0c\u4f46\u4e5f\u4f1a\u5e26\u6765\u4e00\u4e9b\u6027\u80fd\u5f00\u9500\uff0c\u7279\u522b\u662f\u5728\u9891\u7e41\u8c03\u7528\u865a\u51fd\u6570\u7684\u60c5\u51b5\u4e0b\u3002\u56e0\u6b64\uff0c\u5728\u6027\u80fd\u654f\u611f\u7684\u573a\u5408\uff0c\u9700\u8981\u6743\u8861\u4f7f\u7528\u591a\u6001\u6027\u7684\u5229\u5f0a\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u865a\u51fd\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u865a\u51fd\u6570<\/strong>\uff08Virtual Function\uff09\u662f\u4e00\u4e2a\u5728\u57fa\u7c7b\u4e2d\u4f7f\u7528\u5173\u952e\u5b57\u00a0<code>virtual<\/code>\u00a0\u58f0\u660e\u7684\u6210\u5458\u51fd\u6570\u3002\u5b83\u5141\u8bb8\u6d3e\u751f\u7c7b\u5bf9\u5176\u8fdb\u884c\u91cd\u5199\uff0c\u4ece\u800c\u5b9e\u73b0\u8fd0\u884c\u65f6\u591a\u6001\u6027\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u5f62\u5f0f\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><span style=\"background-color: initial; font-family: inherit; font-size: inherit; color: initial;\">virtual<\/span> \u8fd4\u56de\u503c \u51fd\u6570\u540d   \/\/\u57fa\u7c7b\n\u8fd4\u56de\u503c \u51fd\u6570\u540d override  \/\/\u6d3e\u751f\u7c7b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f8b\u5982:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virtual void func();   \/\/\u57fa\u7c7b\nvoid func() override;  \/\/\u6d3e\u751f\u7c7b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8981\u6ce8\u610f\u7684\u4e00\u70b9\u662f\uff0c\u6d3e\u751f\u7c7b\u7684<code>override<\/code>\u5173\u952e\u5b57\u5e76\u4e0d\u662f\u5f3a\u5236\u7684\uff0c\u5b83\u662f\u663e\u5f0f\u63d0\u9192\u7f16\u8bd1\u5668\u8fd9\u662f\u4e00\u4e2a\u865a\u51fd\u6570\u91cd\u5199\uff0c\u867d\u7136\u8fd9\u4e2a\u5173\u952e\u5b57\u53ef\u4ee5\u53bb\u6389\uff0c\u4f46\u662f\u5f3a\u70c8\u5efa\u8bae\u5199\u4e0a\uff0c\u56e0\u4e3a\u5982\u679c\u5f00\u53d1\u8005\u9519\u8bef\u7684\u62fc\u5199\u4e86\u91cd\u5199\u7684\u865a\u51fd\u6570\u7684\u540d\u79f0\uff0c\u5bfc\u81f4\u548c\u57fa\u7c7b\u7684\u51fd\u6570\u7b7e\u540d\u4e0d\u540c\uff0c\u90a3\u4e48\u7f16\u8bd1\u5668\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b91<\/strong>\uff1a\u4e3a\u4ec0\u4e48\u8981\u5f15\u5165\u865a\u51fd\u6570<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u865a\u51fd\u6570\u662f\u4e3a\u4e86\u8ba9\u7236\u7c7b\u5bf9\u5b50\u7c7b\u91cd\u5199\u7684\u51fd\u6570\u8fdb\u884c\u8bbf\u95ee\uff0c\u4e3e\u4e00\u4e2a\u60c5\u666f\u4f8b\u5b50\uff0c\u5047\u8bbe\u4f60\u5728\u5f00\u53d1\u4e00\u4e2a\u6e38\u620f\uff0c\u4f60\u9700\u8981\u4e3a\u6e38\u620f\u7684\u4eba\u7269\u5212\u5206\u51fa\u4e0d\u540c\u7684\u804c\u4e1a\uff0c\u6bd4\u5982\u67aa\u624b\uff0c\u5251\u5ba2\u7b49\u7b49\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u7684\u64cd\u4f5c\u903b\u8f91\uff0c\u53ea\u4e0d\u8fc7\u7279\u6027\u4e0d\u4e00\u6837\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\nclass character{\npublic:\n    void func(){\n        cout&lt;&lt;\"\u6211\u662f\u4eba\u7269\"&lt;&lt;endl;\n    }\n};\n\nclass swordsman : public character{\npublic:\n    void func(){\n        cout&lt;&lt;\"\u6211\u662f\u5251\u5ba2\"&lt;&lt;endl;\n    }\n};\n\nint main(){\n    \n    character a;\n    a.func();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u6784\u5efa\u4e86\u4e00\u4e2a\u4eba\u7269\u7c7b\u548c\u4e00\u4e2a\u5251\u5ba2\u7c7b\uff0c\u4f46\u662f\u5b58\u5728\u4e00\u4e2a\u4e25\u91cd\u7684\u95ee\u9898\uff1a\u4eba\u7269\u7c7b\u65e0\u6cd5\u8bbf\u95ee\u5230\u5251\u5ba2\u7c7b\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u6211\u7684\u4eba\u7269\u7c7b\u591a\u4f59\u4e86\uff0c\u60f3\u8981\u6784\u5efa\u4e00\u4e2a\u89d2\u8272\u5fc5\u987b\u76f4\u63a5\u4f7f\u7528\u804c\u4e1a\u7684\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6240\u4ee5\u6709\u4eba\u4f1a\u63d0\u51fa\u8fd9\u6837\u7684\u89c2\u70b9\uff1a\u6211\u4eec\u53ef\u4ee5\u653e\u5f03\u4eba\u7269\u7c7b\uff0c\u800c\u662f\u4e8b\u5148\u89c4\u5212\u597d\u6240\u6709\u7684\u804c\u4e1a\u7c7b\uff0c\u7136\u540e\u5206\u522b\u5199\u51fa\u4e0d\u540c\u7684\u7c7b\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u5c11\u91cf\u7684\u4ee3\u7801\u4e2d\u6216\u8bb8\u80fd\u884c\u901a\uff0c\u4f46\u662f\u5728\u5927\u5de5\u7a0b\u4e2d\uff0c\u4e5f\u8bb8\u67d0\u5929\u7532\u65b9\u8bf4\u8fd9\u4e2a\u65b9\u6848\u4e34\u65f6\u4fee\u6539\uff0c\u8981\u518d\u52a0\u4e00\u4e2a\u804c\u4e1a\uff0c\u90a3\u4e48\u5728\u6d77\u91cf\u7684\u4ee3\u7801\u4e2d\u4fee\u6539\u6240\u6709\u7684\u804c\u4e1a\u903b\u8f91\u7b80\u76f4\u662f\u4e00\u4e2a\u707e\u96be\u3002\u6240\u4ee5\u8fd9\u4e2a\u89c2\u70b9\u884c\u4e0d\u901a\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u6709\u4eba\u63d0\u51fa\u6765\u53c8\u4e00\u4e2a\u89c2\u70b9\uff1a\u4e0d\u5982\u8bbe\u8ba1\u4e00\u4e2a\u516c\u7528\u7684\u4eba\u7269\u63a5\u53e3\uff0c\u5f53\u7528\u6237\u9009\u62e9\u54ea\u4e00\u4e2a\u804c\u4e1a\u7684\u65f6\u5019\uff0c\u8fd9\u4e2a\u516c\u7528\u63a5\u53e3\u5c31\u4f1a\u5339\u914d\u5230\u8fd9\u4e2a\u7c7b\uff0c\u800c\u4e14\u8fd9\u6837\u5373\u4fbf\u662f\u65b0\u52a0\u804c\u4e1a\u6216\u8005\u53bb\u6389\u4e00\u4e2a\u804c\u4e1a\uff0c\u90fd\u53ea\u9700\u8981\u628a\u7c7b\u5220\u4e86\u5c31\u884c\uff0c\u4e0d\u9700\u8981\u52a8\u6574\u4e2a\u903b\u8f91\uff0c\u56e0\u4e3a\u903b\u8f91\u662f\u6309\u7167\u516c\u7528\u7c7b\u5199\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e8e\u662f\u865a\u51fd\u6570\u5c31\u8bde\u751f\u4e86\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\nclass character{\npublic:\n    virtual void func(){\n        cout&lt;&lt;\"\u6211\u662f\u4eba\u7269\"&lt;&lt;endl;\n    }\n};\n\nclass swordsman : public character{\npublic:\n    void func() override{\n        cout&lt;&lt;\"\u6211\u662f\u5251\u5ba2\"&lt;&lt;endl;\n    }\n};\n\nint main(){\n    \n    character* a = new swordsman;\n    a->func();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u6267\u884c\u7ed3\u679c<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6211\u662f\u5251\u5ba2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u8fd9\u662f\u4fee\u6539\u4e4b\u540e\u7684\u4ee3\u7801\uff0c\u53ef\u4ee5\u770b\u5230\u4eba\u7269\u7c7b\u53ef\u4ee5\u50cf\u8c03\u7528\u81ea\u5df1\u7684\u51fd\u6570\u4e00\u6837\u8c03\u7528\u6d3e\u751f\u7c7b\u7684\u6210\u5458\u51fd\u6570\uff0c\u8fd9\u4e2a\u65f6\u5019\u4eba\u7269\u7c7b\u5c31\u662f\u4e00\u4e2a\u516c\u7528\u7684\u63a5\u53e3\u4e86\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u6838\u5fc3\u70b92<\/strong>\uff1a\u4e3a\u4ec0\u4e48\u7236\u7c7b\u7684\u6307\u9488\u53ef\u4ee5\u7528\u6765\u63a5\u4f4f\u5b50\u7c7b\u7684\u52a8\u6001\u7a7a\u95f4<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4e0a\u8ff0\u4f8b\u5b50\u4e2d\u6709\u4e00\u4e2a\u532a\u5937\u6240\u601d\u7684\u60c5\u51b5\uff0c\u90a3\u5c31\u662f<code>character* a = new swordsman;<\/code>\u4ee3\u7801\uff0c\u5728\u76f4\u89c9\u4e2d\uff0c\u7c7b\u4e0e\u7c7b\u4e4b\u95f4\u7684\u5dee\u8ddd\u5de8\u5927\uff0c\u800c\u4e14\u4e0d\u540c\u7684\u7c7b\u6570\u636e\u4e5f\u4e0d\u4e00\u6837\u7b49\uff0c\u8fd9\u4e9b\u6742\u7cc5\u5728\u4e00\u8d77\uff0c\u5c31\u5bb9\u6613\u7ed9\u521d\u5b66\u8005\u5e26\u6765\u4e00\u79cd\u4e0d\u660e\u767d\uff0c\u4f46\u662f\u60f3\u95ee\u5374\u53c8\u611f\u89c9\u81ea\u5df1\u4e0d\u77e5\u9053\u81ea\u5df1\u54ea\u4e2a\u5730\u65b9\u4e0d\u660e\u767d\u7684\u60c5\u51b5\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9e\u9645\u4e0a\uff0c\u5728\u6709c\u8bed\u8a00\u7684\u57fa\u7840\u65f6\u5c31\u80fd\u610f\u8bc6\u5230\u4e00\u70b9\u2014\u2014\u52a8\u6001\u5185\u5b58\u662f\u4e0d\u77e5\u9053\u81ea\u5df1\u5b58\u4e86\u4ec0\u4e48\u4e1c\u897f\uff0c\u5b83\u53ea\u77e5\u9053\u81ea\u5df1\u5360\u7528\u4e86\u591a\u5c11\u5b57\u8282\u7684\u7a7a\u95f4\u3002\u4e5f\u5c31\u662f\u8bf4\uff0c\u672c\u8d28\u4e0a\u4e00\u4e2a\u52a8\u6001\u5185\u5b58\u7a7a\u95f4\u662f\u5141\u8bb8\u4efb\u610f\u7c7b\u578b\u7684\u6570\u636e\u8bbf\u95ee\uff0c\u4f46\u662fc++\u6bd4\u8f83\u4e25\u683c\uff0c\u5fc5\u987b\u8981\u663e\u5f0f\u58f0\u660e\u6570\u636e\u7c7b\u578b\u7684\u8f6c\u6362\u3002\u6240\u4ee5\u8fd9\u4e2a\u4e5f\u51b3\u5b9a\u4e86\u8fd9\u7bc7\u5185\u5b58\u4e0d\u7ba1\u8c01\u662f\u5f00\u8f9f\u8005\uff0c\u90fd\u4e0d\u4f1a\u5728\u91ca\u653e\u7684\u65f6\u5019\u51fa\u73b0\u5185\u5b58\u6cc4\u9732\u3002\u56e0\u4e3a\u7a7a\u95f4\u8bb0\u4f4f\u7684\u662f\u5185\u5b58\u957f\u5ea6\u800c\u4e0d\u662f\u5185\u5b58\u7ed3\u6784\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u800c\u5728\u8fd9\u4e2a\u4ee3\u7801\u4e2d\uff0c\u5251\u5ba2\u7c7b\u786e\u5b9e\u5c06\u6240\u6709\u7684\u6570\u636e\u5b58\u5165\u4e86\u8fd9\u7247\u5185\u5b58\uff0c\u4f46\u662f\u4eba\u7269\u7c7b\u63a5\u4f4f\u4e86\u8fd9\u4e2a\u5730\u5740\uff0c\u4eba\u7269\u7c7b\u7531\u4e8e\u6ca1\u6709\u6d3e\u751f\u7c7b\u7684\u53d8\u91cf\u3002\u6240\u4ee5\u5728\u6b63\u5e38\u60c5\u51b5\u4e0b\u53ea\u8981\u6d3e\u751f\u7c7b\u5b9a\u4e49\u4e86\u65b0\u7684\u53d8\u91cf\uff0c\u90a3\u4e48\u4eba\u7269\u7c7b\u662f\u65e0\u6cd5\u8bbf\u95ee\u7684\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4f46\u662f\u91cd\u70b9\u6765\u4e86\uff1a\u4eba\u7269\u7c7b\u4e8b\u5148\u5b9a\u4e49\u4e86\u4e00\u7ec4\u63a5\u53e3\uff0c\u5b83\u662f\u4e00\u5b9a\u80fd\u8bbf\u95ee\u8fd9\u4e9b\u63a5\u53e3\u7684\uff0c\u800c\u865a\u51fd\u6570\u6d3e\u751f\u7c7b\u7684\u521d\u8877\u5c31\u662f\u91cd\u5199\u8fd9\u4e2a\u63a5\u53e3\uff0c\u56f4\u7ed5\u7740\u8fd9\u4e2a\u63a5\u53e3\u8fdb\u884c\u4e8c\u6b21\u5f00\u53d1\u3002\u6240\u4ee5\u6211\u7684\u4eba\u7269\u7c7b\u5b8c\u5168\u4e0d\u9700\u8981\u5728\u610f\u6d3e\u751f\u7c7b\u5b9a\u4e49\u4e86\u4ec0\u4e48\u6570\u636e\uff0c\u5b83\u4e0d\u5728\u4e4e\u4e5f\u4e0d\u9700\u8981\u5728\u4e4e\uff0c\u56e0\u4e3a\u4e0d\u7ba1\u5b9a\u4e49\u4ec0\u4e48\u6570\u636e\uff0c\u90fd\u9700\u8981\u88ab\u5b50\u7c7b\u91cd\u5199\u7684\u865a\u51fd\u6570\u4f7f\u7528\uff0c\u7136\u540e\u7236\u7c7b\u8c03\u7528\u5b50\u7c7b\uff0c\u90a3\u4e48\u5b50\u7c7b\u7684\u6570\u636e\u5c31\u4f1a\u88ab\u4f7f\u7528\u5230\u3002\u5c31\u50cf\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\uff0c\u4f60\u4ece\u6765\u4e0d\u9700\u8981\u5173\u7cfb<code>printf<\/code> \u5185\u90e8\u7684\u5c40\u90e8\u53d8\u91cf\u662f\u4ec0\u4e48\uff0c\u56e0\u4e3a\u5b83\u5df2\u7ecf\u63d0\u4f9b\u4e86\u4e00\u4e2a\u63a5\u53e3\u51fd\u6570\u662f<code>printf<\/code>\uff0c\u81f3\u4e8e\u6d3e\u751f\u7c7b\u5b9a\u4e49\u7684\u6210\u5458\u53d8\u91cf\uff0c\u90a3\u5c31\u662f\u6d3e\u751f\u7c7b\u81ea\u5df1\u7684\u4e8b\u60c5\u4e86\uff0c\u4e0d\u7ba1\u5b83\u600e\u4e48\u7528\uff0c\u53ea\u8981\u4fdd\u8bc1\u5b9e\u73b0\u7684\u51fd\u6570\u529f\u80fd\u80fd\u8dd1\u5c31\u884c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">\u865a\u6790\u6784\u51fd\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u4f7f\u7528\u4e86\u865a\u51fd\u6570\u7684\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u4e00\u4e2a\u60c5\u51b5\uff0c\u90a3\u5c31\u662f\u57fa\u7c7b\u4e2d\u5360\u7528\u4e86\u8d44\u6e90\uff0c\u4f46\u662f\u7531\u4e8e\u57fa\u7c7b\u7684\u6307\u9488\u672c\u8d28\u4e0a\u6307\u5411\u7684\u662f\u6d3e\u751f\u7c7b\u7684\u7a7a\u95f4\uff0c\u5728\u7a7a\u95f4\u91ca\u653e\u540e\uff0c\u53ea\u4f1a\u91ca\u653e\u6d3e\u751f\u7c7b\u800c\u4e0d\u662f\u91ca\u653e\u57fa\u7c7b\uff0c\u90a3\u4e48\u5c31\u9700\u8981\u4e00\u79cd\u65b9\u5f0f\u8c03\u7528\u57fa\u7c7b\u81ea\u5df1\u7684\u6790\u6784\u51fd\u6570\u3002\u8fd9\u5c31\u662f\u865a\u6790\u6784\u51fd\u6570\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u6807\u51c6\u683c\u5f0f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virtual \u6790\u6784\u51fd\u6570  \/\/\u57fa\u7c7b<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\nclass character{\npublic:\n    virtual void func(){\n        cout&lt;&lt;\"\u6211\u662f\u4eba\u7269\"&lt;&lt;endl;\n        p = new int;\n    }\n    virtual ~character(){\n        cout&lt;&lt;\"\u91ca\u653e\u4eba\u7269\u7c7b\"&lt;&lt;endl;\n        delete p;\n    }\nprivate:\n    int* p;\n};\n\nclass swordsman : public character{\npublic:\n    void func() override{\n        cout&lt;&lt;\"\u6211\u662f\u5251\u5ba2\"&lt;&lt;endl;\n        p = new int;\n    }\n    ~swordsman(){\n        cout&lt;&lt;\"\u91ca\u653e\u5251\u5ba2\u7c7b\"&lt;&lt;endl;\n        delete p;\n    }\nprivate:\n    int *p;\n};\n\nint main(){\n    character* a = new swordsman;\n    a->func();\n    delete a;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u6211\u662f\u5251\u5ba2\n\u91ca\u653e\u5251\u5ba2\u7c7b\n\u91ca\u653e\u4eba\u7269\u7c7b<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u62bd\u8c61\u7c7b\u4e0e\u7eaf\u865a\u51fd\u6570<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u7eaf\u865a\u51fd\u6570<\/strong>\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u865a\u51fd\u6570\uff0c\u5b83\u5728\u57fa\u7c7b\u4e2d\u58f0\u660e\u4f46\u6ca1\u6709\u5177\u4f53\u5b9e\u73b0\u3002\u7eaf\u865a\u51fd\u6570\u7684\u58f0\u660e\u5f62\u5f0f\u662f\u5728\u51fd\u6570\u58f0\u660e\u540e\u6dfb\u52a0\u00a0<code>= 0<\/code>\u3002\u4f8b\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class Base {\npublic:\n    virtual void pureVirtualFunction() = 0; \/\/ \u7eaf\u865a\u51fd\u6570\n};<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>\u62bd\u8c61\u7c7b<\/strong>\u662f\u5305\u542b\u81f3\u5c11\u4e00\u4e2a\u7eaf\u865a\u51fd\u6570\u7684\u7c7b\u3002\u62bd\u8c61\u7c7b\u4e0d\u80fd\u5b9e\u4f8b\u5316\u5bf9\u8c61\uff0c\u53ea\u80fd\u4f5c\u4e3a\u57fa\u7c7b\u4f7f\u7528\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e0d\u8fc7\u8981\u8865\u5145\u4e00\u70b9\uff1a\u7eaf\u865a\u51fd\u6570\u5c31\u50cf\u662f\u4e00\u79cd\u4f20\u67d3\u6027\u6781\u5f3a\u7684\u75c5\u6bd2\uff0c\u5982\u679c\u6d3e\u751f\u7c7b\u7ee7\u627f\u4e86\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u4f46\u662f\u6ca1\u6709\u5b8c\u5168\u91cd\u5199\u6240\u6709\u7eaf\u865a\u51fd\u6570\uff0c\u90a3\u4e48\u8fd9\u4e2a\u6d3e\u751f\u7c7b\u4f1a\u88ab\u8f6c\u5316\u4e3a\u4e00\u4e2a\u62bd\u8c61\u7c7b\uff0c\u4ece\u800c\u4e5f\u65e0\u6cd5\u5b9e\u4f8b\u5316\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e00\u4e2a\u4f8b\u5b50<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\nclass A{\npublic:\n    virtual void func_1() = 0;\n    virtual void func_2() = 0;\n};\n\nclass B: public A{\npublic:\n    void func_1() override{\n        cout&lt;&lt;\"\u8fd9\u662fB\u7684func1\"&lt;&lt;endl;\n    }\n};\n\nclass C:public B{\npublic:\n    void func_1() override{\n        cout&lt;&lt;\"\u8fd9\u662fC\u7684func1\"&lt;&lt;endl;\n    }\n    void func_2() override{\n        cout&lt;&lt;\"\u8fd9\u662fC\u7684func2\"&lt;&lt;endl;\n    }\n};\n\nint main(){\n   A *a = new C;\n   a->func_1();\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u6267\u884c\u7ed3\u679c<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u8fd9\u662fC\u7684func1<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0cB\u7c7b\u5c31\u88ab\u611f\u67d3\u6210\u4e86\u62bd\u8c61\u7c7b\uff0c\u5b83\u65e0\u6cd5\u5b9e\u4f8b\u5316\uff0c\u5426\u5219\u4f1a\u62a5\u9519\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\"><kbd>\u8865\u5145<\/kbd><\/h1>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>\u53cb\u5143\u51fd\u6570\u548c\u53cb\u5143\u7c7b<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728C++\u4e2d\uff0c\u53cb\u5143\uff08Friend\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5b83\u867d\u7136\u5b9a\u4e49\u5728\u7c7b\u7684\u5916\u90e8\uff0c\u4f46\u53ef\u4ee5\u8bbf\u95ee\u8be5\u7c7b\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002\u53cb\u5143\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57 friend\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cb\u5143\u7684\u4e3b\u8981\u76ee\u7684\u662f\u63d0\u4f9b\u4e00\u79cd\u673a\u5236\uff0c\u4f7f\u5f97\u5916\u90e8\u51fd\u6570\u6216\u7c7b\u80fd\u591f\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002\u8fd9\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u975e\u5e38\u6709\u7528\uff0c\u4f8b\u5982\u9700\u8981\u8bbf\u95ee\u7c7b\u7684\u5185\u90e8\u6570\u636e\u8fdb\u884c\u67d0\u4e9b\u64cd\u4f5c\u65f6\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c24\u5176\u8981\u6ce8\u610f\u7684\u662f\uff1a\u53cb\u5143\u662f\u5355\u5411\u6743\u9650\uff0c\u4e5f\u5c31\u662f\u6211\u628a\u4f60\u5f53\u670b\u53cb\uff0c\u4f60\u4e0d\u4e00\u5b9a\u628a\u6211\u5f53\u670b\u53cb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53cb\u5143\u51fd\u6570<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728C++\u4e2d\uff0c<strong>\u53cb\u5143\u51fd\u6570<\/strong>\uff08Friend Function\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u51fd\u6570\uff0c\u5b83\u867d\u7136\u5b9a\u4e49\u5728\u7c7b\u7684\u5916\u90e8\uff0c\u4f46\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002\u53cb\u5143\u51fd\u6570\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57\u00a0<code>friend<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cb\u5143\u51fd\u6570\u662f\u5728\u7c7b\u7684\u5b9a\u4e49\u4e2d\u4f7f\u7528\u00a0<code>friend<\/code>\u00a0\u5173\u952e\u5b57\u58f0\u660e\u7684\u51fd\u6570\u3002\u5b83\u53ef\u4ee5\u662f\u5168\u5c40\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u662f\u5176\u4ed6\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u5176\u6b21\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:#fcb900\" class=\"has-inline-color\">\u793a\u4f8b\uff1a<\/mark><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\n\nclass Box {\nprivate:\n    double width;\npublic:\n    Box(double w) : width(w) {}\n    \/\/ \u58f0\u660e\u53cb\u5143\u51fd\u6570\n    friend void printWidth(Box box);\n};\n\n\/\/ \u53cb\u5143\u51fd\u6570\u5b9a\u4e49\nvoid printWidth(Box box) {\n    \/\/ \u53ef\u4ee5\u8bbf\u95ee Box \u7c7b\u7684\u79c1\u6709\u6210\u5458\n    cout &lt;&lt; \"Width of box: \" &lt;&lt; box.width &lt;&lt; endl;\n}\n\nint main() {\n    Box box(10.0);\n    printWidth(box); \/\/ \u8f93\u51fa\uff1aWidth of box: 10\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cb\u5143\u51fd\u6570\u4e5f\u53ef\u4ee5\u662f\u53e6\u4e00\u4e2a\u7c7b\u7684\u6210\u5458\u51fd\u6570\u3002\u6bd4\u5982\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>class AnotherClass {\npublic:\n    void display(Box&amp; box);\n};\n\nclass Box {\nprivate:\n    double width;\npublic:\n    Box(double w) : width(w) {}\n    \/\/ \u58f0\u660e AnotherClass \u7684\u6210\u5458\u51fd\u6570\u4e3a\u53cb\u5143\n    friend void AnotherClass::display(Box&amp; box);\n};\n\nvoid AnotherClass::display(Box&amp; box) {\n    cout &lt;&lt; \"Width of box: \" &lt;&lt; box.width &lt;&lt; endl;\n}<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u53cb\u5143\u51fd\u6570\u7684\u4f18\u7f3a\u70b9\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f18\u70b9<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u63d0\u4f9b\u4e86\u4e00\u79cd\u8bbf\u95ee\u7c7b\u79c1\u6709\u6210\u5458\u7684\u7075\u6d3b\u65b9\u5f0f\u3002<\/li>\n\n\n\n<li>\u53ef\u4ee5\u5b9e\u73b0\u67d0\u4e9b\u9700\u8981\u8bbf\u95ee\u7c7b\u5185\u90e8\u6570\u636e\u7684\u529f\u80fd\uff0c\u800c\u4e0d\u7834\u574f\u7c7b\u7684\u5c01\u88c5\u6027\u3002<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>\u7f3a\u70b9<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li>\u7834\u574f\u4e86\u7c7b\u7684\u5c01\u88c5\u6027\uff0c\u56e0\u4e3a\u53cb\u5143\u51fd\u6570\u53ef\u4ee5\u8bbf\u95ee\u7c7b\u7684\u79c1\u6709\u6210\u5458\u3002<\/li>\n\n\n\n<li>\u53ef\u80fd\u5bfc\u81f4\u4ee3\u7801\u7684\u53ef\u7ef4\u62a4\u6027\u964d\u4f4e\uff0c\u56e0\u4e3a\u53cb\u5143\u51fd\u6570\u7684\u4f7f\u7528\u589e\u52a0\u4e86\u7c7b\u4e4b\u95f4\u7684\u8026\u5408\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">\u53cb\u5143\u7c7b<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u5728C++\u4e2d\uff0c<strong>\u53cb\u5143\u7c7b<\/strong>\uff08Friend Class\uff09\u662f\u4e00\u79cd\u7279\u6b8a\u7684\u7c7b\uff0c\u5b83\u53ef\u4ee5\u8bbf\u95ee\u53e6\u4e00\u4e2a\u7c7b\u7684\u6240\u6709\u6210\u5458\uff0c\u5305\u62ec\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\u3002\u53cb\u5143\u7c7b\u7684\u58f0\u660e\u9700\u8981\u4f7f\u7528\u5173\u952e\u5b57\u00a0<code>friend<\/code>\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5f53\u4e00\u4e2a\u7c7b\u88ab\u58f0\u660e\u4e3a\u53e6\u4e00\u4e2a\u7c7b\u7684\u53cb\u5143\u7c7b\u65f6\uff0c\u8fd9\u4e2a\u53cb\u5143\u7c7b\u7684\u6240\u6709\u6210\u5458\u51fd\u6570\u90fd\u53ef\u4ee5\u8bbf\u95ee\u8be5\u7c7b\u7684\u79c1\u6709\u6210\u5458\u548c\u4fdd\u62a4\u6210\u5458\uff0c\u53cb\u5143\u7c7b\u63d0\u4f9b\u4e86\u4e00\u79cd\u673a\u5236\uff0c\u4f7f\u5f97\u4e24\u4e2a\u7c7b\u4e4b\u95f4\u53ef\u4ee5\u5171\u4eab\u79c1\u6709\u6570\u636e\uff0c\u901a\u5e38\u7528\u4e8e\u5b9e\u73b0\u7d27\u5bc6\u8026\u5408\u7684\u7c7b\u4e4b\u95f4\u7684\u534f\u4f5c\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4e00\u4e2a\u4f8b\u5b50\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\nusing namespace std;\n\nclass Box {\nprivate:\n    double width;\npublic:\n    Box(double w) : width(w) {}\n    \/\/ \u58f0\u660e\u53cb\u5143\u7c7b\n    friend class BoxFriend;\n};\n\nclass BoxFriend {\npublic:\n    void printWidth(Box&amp; box) {\n        \/\/ \u53ef\u4ee5\u8bbf\u95ee Box \u7c7b\u7684\u79c1\u6709\u6210\u5458\n        cout &lt;&lt; \"Width of box: \" &lt;&lt; box.width &lt;&lt; endl;\n    }\n};\n\nint main() {\n    Box box(10.0);\n    BoxFriend bf;\n    bf.printWidth(box); \/\/ \u8f93\u51fa\uff1aWidth of box: 10\n    return 0;\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u5728\u5b66\u4e60C\u548cC++\u7684\u8fc7\u7a0b\u4e2d\uff0c\u8001\u5e08\u901a\u5e38\u4f1a\u7b3c\u7edf\u5730\u8bf4C++\u5305\u542b\u4e86C\uff0c\u4e5f\u5c31\u662f\u90a3\u53e5\u719f\u6089\u7684\u8bdd\u2014\u2014C++\u662fC\u7684\u8d85\u96c6\u3002\u8fd9\u79cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,1],"tags":[17,42],"class_list":["post-1133","post","type-post","status-publish","format-standard","hentry","category-c-c","category-learning","tag-c-2","tag-42"],"_links":{"self":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1133","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1133"}],"version-history":[{"count":24,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1133\/revisions"}],"predecessor-version":[{"id":1177,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/posts\/1133\/revisions\/1177"}],"wp:attachment":[{"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1133"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1133"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.chxzking.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1133"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}