重载(Overload)编程
- 同一个标识符在不一样的上下文有不一样的意义
如:函数
思考:重载在天然语言中是随处可见的,那么程序设计中是否也有重载呢?debug
- 用同一个函数名定义不一样的函数
- 当函数名和不一样的参数搭配时函数的意义不一样
#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; }
输出: 3 9 12
函数重载至少知足下面的一个条件设计
- 参数个数不一样
- 参数类型不一样
- 参数顺序不一样
int func(int a, const char* s) { return a; } int func(const char* s, int a) { return strlen(s); }
- 当函数默认参数赶上函数重载会发生什么?
#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 ambiguous candidates are: int func(int, int, int) note: int func(int, int)
编译器调用重载函数的准则code
- 将全部同名函数做为候选者
尝试寻找可行的候选函数ci
- 精准匹配实参
- 经过默认参数可以匹配实参
- 经过默认类型转换匹配实参
匹配失败编译器
- 最终寻找到的候选函数不惟一,则出现二义性,编译失败
- 没法匹配全部候选者,函数未定义,编译失败
函数重载的注意事项string
- 重载函数在本质上是互相独立的不一样函数
- 重载函数的函数类型不一样
- 函数返回值不能做为函数重载的依据
函数重载由函数名和参数列表决定!it
#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; }
输出: 00871005 0087100F
vc2010编译器符号表:io
File Type: COFF OBJECT COFF SYMBOL TABLE 000 00AB766F ABS notype Static | @comp.id 001 00000001 ABS notype Static | @feat.00 002 00000000 SECT1 notype Static | .drectve Section length 30, #relocs 0, #linenums 0, checksum 0 004 00000000 SECT2 notype Static | .debug$S Section length 4F8, #relocs 0, #linenums 0, checksum 0 006 00000000 SECT3 notype Static | .rdata Section length 8, #relocs 0, #linenums 0, checksum C59740A9 008 00000000 SECT3 notype Static | $SG5270 009 00000004 SECT3 notype Static | $SG5275 00A 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$TMZ 012 00000000 UNDEF notype () External | __RTC_Shutdown 013 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$IMZ 016 00000000 UNDEF notype () External | __RTC_InitBase 017 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 | _main 021 00000000 UNDEF notype External | __imp__printf 022 00000000 UNDEF notype () External | __RTC_CheckEsp 023 00000000 SECTC notype Static | .debug$T Section length 50, #relocs 0, #linenums 0, checksum 0 String 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++ 中经过函数名和函数参数肯定函数调用
以上内容参考狄泰软件学院系列课程,请你们保护原创!