当前位置:网站首页>7.2-function-overloading
7.2-function-overloading
2022-07-29 05:07:00 【学习make me happy】
1背景-不同类型的四舍五入-C和C++的实现

2 函数重载实现function overloading fulfillment
- The compiler will preform name lookup
- Argument-dependent lookup, also known as ADL.
- The return type will not be considered in name lookup.
That is to say, 相同的函数必须是函数名和参数列表都相同的函数。
3 实例讲解
#include <iostream>
using namespace std;
int sum(int x, int y)
{
cout << "sum(int, int) is called" << endl;
return x + y;
}
float sum(float x, float y)
{
cout << "sum(float, float) is called" << endl;
return x + y;
}
double sum(double x, double y)
{
cout << "sum(double, double) is called" << endl;
return x + y;
}
// //Is the following definition correct?
// double sum(int x, int y)
// {
// cout << "sum(int, int) is called" << endl;
// return x + y;
// }
int main()
{
cout << "sum = " << sum(1, 2) << endl;
cout << "sum = " << sum(1.1f, 2.2f) << endl;
cout << "sum = " << sum(1.1, 2.2) << endl;
//which function will be called?
cout << "sum = " << sum(1, 2.2) << endl;
return 0;
}
于老师课上讲的两个例子
- 一个是函数名相同,输入参数相同,返回参数不同,这个就是函数重定义,编译器报错。
- 另一个例子是当参数类型不同的时候,如果有多个函数,编译器会报错。但如果只有一个函数,不报错。
point
函数重载可以减轻程序员的记忆负担。
边栏推荐
- Mapper agent development
- [wechat applet] swiper slides the page, and the left and right sides of the slider show part of the front and back, showing part of the front and back
- 电脑无法打开excel表格怎么办?excel打不开的解决方法
- How does excel filter out the content you want? Excel table filtering content tutorial
- JS daily question (10)
- A little knowledge about management
- The song of the virtual idol was originally generated in this way!
- JS (foreach) return cannot end the function solution
- [untitled]
- 荣耀2023内推,内推码ambubk
猜你喜欢

Unity Metaverse(三)、Protobuf & Socket 实现多人在线

2021-10-23

Force deduction ----- sort odd and even subscripts respectively

JS (foreach) return cannot end the function solution

基于注解的三层项目的改造及添加包扫描的方式

那个准时上下班,从不愿意加班加点的人,在我前面升职了...

excel怎么设置行高和列宽?excel设置行高和列宽的方法

< El table column> place multiple pictures

MySQL regularly calls preset functions to complete data update

Functions in MySQL statements
随机推荐
WPS如何进行快速截屏?WPS快速截屏的方法
How does WPS use smart fill to quickly fill data? WPS method of quickly filling data
MySQL many to many relationship, grouping and splicing to query multiple data to one data
[config] configure array parameters
About realizing page Jump of website in Servlet
Self join and joint query of MySQL
Sparksql inserts or updates in batches and saves data to MySQL
Legend how to configure multiple versions of wechat updates on one server
基于注解的三层项目的改造及添加包扫描的方式
电脑无法打开excel表格怎么办?excel打不开的解决方法
浅谈AspectJ框架
Pivot table of odoo development tutorial
三层项目的架构分析及构造方法的参数名称注入
TCP three handshakes and four waves
How to add a map to the legendary server
ODOO开发教程之图表
带你搞懂 Kubernetes 集群中几种常见的流量暴露方案
roLabelImg转DATO格式数据
AUTOSAR从入门到精通100讲(七十八)-AUTOSAR-DEM模块
传奇如何一台服务器配置多个版本微端更新