重载的概念重载(Overload)同一个标识符在不同的上下文有不同的意义如:“洗"和不同的词汇搭配后有不同的含义: 洗衣服,洗脸,洗脑,洗马桶,。。。“play"和不同的单词搭配后有不同的含义:play chess, play piano, paly basketball,…思考:重载在自然语言中是随处可见的,那么程序设计中是否也有重载呢?C++ 中的函数重载(Function Overload)用同一个函数名定义不同的函数当函数名和不同的参数搭配是函数的意义不同编程实验: 函数重载实验#include <stdio.h>#include <string.h>int func(int x){ return x;}int func(int a, int b){ return a + b;}int func(const char* s){ return strlen(s);}int main(int argc, char* argv[]){ printf("%d\n”, func(3)); printf("%d\n”, func(4, 5)); printf("%d\n", func(“D.T.Software”)); return 0;}输出:3912函数重载至少满足下面的一个条件参数个数不同参数类型不同参数顺序不同int func(int a, const char* s){ return a;}int func(const char* s, int a){ return strlen(s);}当函数默认参数遇上函数重载会发生什么?编程实验: 函数默认参数 VS 函数重载#include <stdio.h>#include <string.h>int func(int a, int b, int c = 0){ return a * b * c;}int func(int a, int b){ return a + b;}int main(int argc, char* argv[]){ int c = func(1, 2); return 0;}输出:【两重载都可单独匹配,导致匹配出现二义性】error: call of overloaded ‘func(int, int)’ is ambiguouscandidates are: int func(int, int, int)note: int func(int, int)编译器调用重载函数的准则将所有同名函数作为候选者尝试寻找可行的候选函数精准匹配实参通过默认参数能够匹配实参通过默认类型转换匹配实参匹配失败最终寻找到的候选函数不唯一,则出现二义性,编译失败无法匹配所有候选者,函数未定义,编译失败函数重载的注意事项重载函数在本质上是互相独立的不同函数重载函数的函数类型不同函数返回值不能作为函数重载的依据函数重载由函数名和参数列表决定!编程实验: 函数重载的本质#include <stdio.h>int add(int a, int b) // int(int, int){ return a + b;}int add(int a, int b, int c) // int(int, int, int){ return a + b + b;}int main(){ printf("%p\n", (int()(int, int))add); printf("%p\n", (int()(int, int, int))add); return 0;}输出:008710050087100Fvc2010编译器符号表:File Type: COFF OBJECTCOFF SYMBOL TABLE000 00AB766F ABS notype Static | @comp.id001 00000001 ABS notype Static | @feat.00002 00000000 SECT1 notype Static | .drectve Section length 30, #relocs 0, #linenums 0, checksum 0004 00000000 SECT2 notype Static | .debug$S Section length 4F8, #relocs 0, #linenums 0, checksum 0006 00000000 SECT3 notype Static | .rdata Section length 8, #relocs 0, #linenums 0, checksum C59740A9008 00000000 SECT3 notype Static | $SG5270009 00000004 SECT3 notype Static | $SG527500A 00000000 SECT4 notype Static | .text Section length B, #relocs 0, #linenums 0, checksum D0372BF8, selection 1 (pick no duplicates)00C 00000000 SECT5 notype Static | .debug$S Section length DC, #relocs 5, #linenums 0, checksum 0, selection 5 (pick associative Section 0x4)00E 00000000 SECT4 notype () External | ?add@@YAHHH@Z (int __cdecl add(int,int))00F 00000000 SECT6 notype Static | .rtc$TMZ Section length 4, #relocs 1, #linenums 0, checksum 0, selection 5 (pick associative Section 0x4)011 00000000 SECT6 notype Static | __RTC_Shutdown.rtc$TMZ012 00000000 UNDEF notype () External | __RTC_Shutdown013 00000000 SECT7 notype Static | .rtc$IMZ Section length 4, #relocs 1, #linenums 0, checksum 0, selection 5 (pick associative Section 0x4)015 00000000 SECT7 notype Static | __RTC_InitBase.rtc$IMZ016 00000000 UNDEF notype () External | __RTC_InitBase017 00000000 SECT8 notype Static | .text Section length E, #relocs 0, #linenums 0, checksum 3E69ABE4, selection 1 (pick no duplicates)019 00000000 SECT9 notype Static | .debug$S Section length E8, #relocs 5, #linenums 0, checksum 0, selection 5 (pick associative Section 0x8)01B 00000000 SECT8 notype () External | ?add@@YAHHHH@Z (int __cdecl add(int,int,int))01C 00000000 SECTA notype Static | .text Section length 48, #relocs 9, #linenums 0, checksum 8A8FE64F, selection 1 (pick no duplicates)01E 00000000 SECTB notype Static | .debug$S Section length F0, #relocs 9, #linenums 0, checksum 0, selection 5 (pick associative Section 0xA)020 00000000 SECTA notype () External | _main021 00000000 UNDEF notype External | __imp__printf022 00000000 UNDEF notype () External | __RTC_CheckEsp023 00000000 SECTC notype Static | .debug$T Section length 50, #relocs 0, #linenums 0, checksum 0String Table Size = 0x8A bytes Summary 7AC .debug$S 50 .debug$T 30 .drectve 8 .rdata 4 .rtc$IMZ 4 .rtc$TMZ 61 .text函数编译后对应的标识符00E 00000000 SECT4 notype () External | ?add@@YAHHH@Z (int __cdecl add(int,int))01B 00000000 SECT8 notype () External | ?add@@YAHHHH@Z (int __cdecl add(int,int,int))函数地址不同编译器编译后函数所得到的标识符不同==> 重载函数在本质上是互相独立的不同函数小结函数重载是 C++ 中引入的概念函数重载用于模拟自然语言中的词汇搭配函数重载使得 C++ 具有更丰富的语义表达能力函数重载的本质为互相独立的不同函数C++ 中通过函数名和函数参数确定函数调用以上内容参考狄泰软件学院系列课程,请大家保护原创!