【C++】 8_函数重载分析 (上)

重载的概念

  • 重载(Overload)

    • 同一个标识符在不同的上下文有不同的意义

如:

  1. "洗"和不同的词汇搭配后有不同的含义: 洗衣服,洗脸,洗脑,洗马桶,。。。
  2. "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;
}
输出:
3
9
12
  • 函数重载至少满足下面的一个条件

    • 参数个数不同
    • 参数类型不同
    • 参数顺序不同
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 ambiguous
candidates 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;
}
输出:
00871005
0087100F

vc2010编译器符号表:

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))
  1. 函数地址不同
  2. 编译器编译后函数所得到的标识符不同

==> 重载函数在本质上是互相独立的不同函数

小结

  • 函数重载是 C++ 中引入的概念
  • 函数重载用于模拟自然语言中的词汇搭配
  • 函数重载使得 C++ 具有更丰富的语义表达能力
  • 函数重载的本质为互相独立的不同函数
  • C++ 中通过函数名和函数参数确定函数调用

以上内容参考狄泰软件学院系列课程,请大家保护原创!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值