当前位置: 首页 > news >正文

C++系列-函数重载

C++系列-函数重载

  • 函数重载
    • 函数重载的条件
    • 函数重载注意事项
      • 引用作为重载
      • 函数重载遇到默认参数

函数重载

  • 函数名可以相同, 提高复用性

函数重载的条件

  • 同一个作用域下
  • 函数名相同
  • 函数参数不同
    – 参数个数不同
    – 参数顺序不同
    – 参数类型不同
  • 不可以使用返回值作为重载的条件
code:#include<iostream>using namespace std;void test(){cout << "void test()" << endl;}void test(int a){cout << "void test(int a)" << endl;}void test(int a, float b){cout << "void test(int a, float b)" << endl;}void test(float a, int b){cout << "void test(float a, int b)" << endl;}void main(){test();test(100);test(100, 3.14);test(3.14, 100);system("pause");}
result:void test()void test(int a)void test(int a, float b)void test(float a, int b)

函数重载注意事项

引用作为重载

  • 参数可以分为const和非const。
code:#include<iostream>using namespace std;void test(int &a){cout << "void test(int &a)" << endl;}void test(const int& a){cout << "void test(const int& a)" << endl;}void main(){int a = 10;test(a);test(10);		// 当执行void test(int &a) 则为int &a=10,会出错,const int& a=10,正常system("pause");}
result:
void test(int &a)
void test(const int& a)

函数重载遇到默认参数

code:
#include<iostream>
using namespace std;void test(int a, int b = 10)
{cout << "void test(int a, int b = 10)" << endl;
}
void test(int a)
{cout << "void test(int a)" << endl;
}void main()
{//test(666);		// 报错,不知道执行哪一个test(20, 30);system("pause");
}result:void test(int a, int b = 10)
http://www.lryc.cn/news/139262.html

相关文章:

  • leetcode-23.合并k个升序链表-day17
  • Linux scp命令
  • vue 简单实验 v-bind 变量与html属性绑定
  • 114.(cesium篇)cesium去掉时间轴并用按钮控制运动
  • 2023年清洁能源与智能电网国际会议(CCESG 2023)
  • RISC-V中国峰会 | 256核服务器高调亮相,谁与争锋?
  • 树套树小结
  • android 解决sdk代码冲突
  • C++逆天合集
  • stm32之15.超声波与灯光功能一起实现(进阶)
  • 美创科技荣获“2023年网络安全优秀创新成果大赛—杭州分站赛”两项优胜奖
  • 使用gdb+gdbserver远程调试aarch64平台程序
  • 【CesiumJS入门】(9)获取地表两点的距离及中心点——EllipsoidGeodesic
  • OLED透明屏介绍:领先科技的革命性创新
  • ESXI补丁更新
  • 【每日易题】数组下标的逆天用法——你见过把数组存储的值当作数组下标来解题的吗?
  • mysql基本操作
  • vue组件的使用
  • Axure设计之日期选择器(年月选择)
  • CAD泰森多边形框架3D插件
  • Django的render()函数的三个主要参数详解,特别是第三个字典类型的参数context
  • 统计不同字段的值域
  • js this 指的是什么
  • 用pytorch实现Resnet
  • C++类成员的访问权限以及类的封装
  • Linux 多线程解决客户端与服务器端通信
  • FMX的TListBox单选列表框
  • prompt工程(持续更新ing...)
  • win11 docker-desktop安装记录
  • opencv特征提取、梯度计算