一.在C++中我们可能会使用到很多同名但是底层时如何识的呢?
在linux环境下,我们可以查看汇编代码,这个是我们的源代码。
#include <iostream> int swap1(int a,int b){ int temp = a; a = b; b = temp; return 0; } double swap1(double a,double b){ double temp = a; a = b; b = temp; return 2.0; } int main(){ swap1(1,2); swap1(1.0,2.0); return 0; }
我们可以看到有两个同名不同参的函数,然后我们将此文件进行编译。我们来看一下汇编代码。
.file "test.cpp" .text .local _ZStL8__ioinit .comm _ZStL8__ioinit,1,1 .globl _Z5swap1ii .type _Z5swap1ii, @function _Z5swap1ii: .LFB1731: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl %edi, -20(%rbp) movl %esi, -24(%rbp) movl -20(%rbp), %eax movl %eax, -4(%rbp) movl -24(%rbp), %eax movl %eax, -20(%rbp) movl -4(%rbp), %eax movl %eax, -24(%rbp) movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE1731: .size _Z5swap1ii, .-_Z5swap1ii .globl _Z5swap1dd .type _Z5swap1dd, @function _Z5swap1dd: .LFB1732: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movsd %xmm0, -24(%rbp) movsd %xmm1, -32(%rbp) movsd -24(%rbp), %xmm0 movsd %xmm0, -8(%rbp) movsd -32(%rbp), %xmm0 movsd %xmm0, -24(%rbp) movsd -8(%rbp), %xmm0 movsd %xmm0, -32(%rbp) movsd .LC0(%rip), %xmm0 movq %xmm0, %rax movq %rax, %xmm0 popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE1732: .size _Z5swap1dd, .-_Z5swap1dd .globl main .type main, @function main: .LFB1733: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $2, %esi movl $1, %edi call _Z5swap1ii movsd .LC0(%rip), %xmm0 movq .LC1(%rip), %rax movapd %xmm0, %xmm1 movq %rax, %xmm0 call _Z5swap1dd movl $0, %eax popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE1733: .size main, .-main .type _Z41__static_initialization_and_destruction_0ii, @function _Z41__static_initialization_and_destruction_0ii: .LFB2228: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 subq $16, %rsp movl %edi, -4(%rbp) movl %esi, -8(%rbp) cmpl $1, -4(%rbp) jne .L9 cmpl $65535, -8(%rbp) jne .L9 leaq _ZStL8__ioinit(%rip), %rax movq %rax, %rdi call _ZNSt8ios_base4InitC1Ev@PLT leaq __dso_handle(%rip), %rax movq %rax, %rdx leaq _ZStL8__ioinit(%rip), %rax movq %rax, %rsi movq _ZNSt8ios_base4InitD1Ev@GOTPCREL(%rip), %rax movq %rax, %rdi call __cxa_atexit@PLT .L9: nop leave .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE2228: .size _Z41__static_initialization_and_destruction_0ii, .-_Z41__static_initialization_and_destruction_0ii .type _GLOBAL__sub_I__Z5swap1ii, @function _GLOBAL__sub_I__Z5swap1ii: .LFB2229: .cfi_startproc endbr64 pushq %rbp .cfi_def_cfa_offset 16 .cfi_offset 6, -16 movq %rsp, %rbp .cfi_def_cfa_register 6 movl $65535, %esi movl $1, %edi call _Z41__static_initialization_and_destruction_0ii popq %rbp .cfi_def_cfa 7, 8 ret .cfi_endproc .LFE2229: .size _GLOBAL__sub_I__Z5swap1ii, .-_GLOBAL__sub_I__Z5swap1ii .section .init_array,"aw" .align 8 .quad _GLOBAL__sub_I__Z5swap1ii .section .rodata .align 8 .LC0: .long 0 .long 1073741824 .align 8 .LC1: .long 0 .long 1072693248 .hidden __dso_handle .ident "GCC: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0" .section .note.GNU-stack,"",@progbits .section .note.gnu.property,"a" .align 8 .long 1f - 0f .long 4f - 1f .long 5 0: .string "GNU" 1: .align 8 .long 0xc0000002 .long 3f - 2f 2: .long 0x3 3: .align 8 4:
我们可以从中看到两个相似的东西
_Z5swap1dd:
_Z5swap1ii:文章来源:https://www.toymoban.com/news/detail-732716.html
这个就是我们的两个函数,5就是说函数名时五个字符(博主没学过汇编,_Z可能时返回值,swap1是我们的函数名,dd和ii分别时我们形参的类型,大家自己去试试其他的)文章来源地址https://www.toymoban.com/news/detail-732716.html
到了这里,关于C++编译时多态是如何实现的呢?(名字粉碎机制)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!