当前位置:网站首页>7.2-function-overloading
7.2-function-overloading
2022-07-29 08:28:00 【Learn make me happy】
1 background - Different types of rounding -C and C++ The implementation of the

2 Function overload implementation 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, The same function must be a function with the same function name and parameter list .
3 Instance to explain
#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;
}
Two examples mentioned in the teacher's class
- One is the same function name , The input parameters are the same , The return parameters are different , This is function redefinition , Compiler error .
- Another example is when the parameter types are different , If there are multiple functions , The compiler will report an error . But if there is only one function , Don't complain .
point
Function overloading can reduce the memory burden of programmers .
边栏推荐
- Security baseline of network security
- Tensorboard use
- GBase 8s数据库有哪些备份恢复方式
- leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
- Brief introduction and use of commonjs import and export and ES6 modules import and export
- Importerror: no module named XX
- Cmake setting vs Startup running environment path
- Hal library learning notes - 8 concept of serial communication
- Proteus simulation based on 51 MCU ADC0808
- Use SQL client How can the job generated by SH achieve breakpoint continuation after cancle?
猜你喜欢

Solve the problem of MSVC2017 compiler with yellow exclamation mark in kits component of QT

Gan: generate adversarial networks

谷歌浏览器免跨域配置

Intelligent shelf safety monitoring system

Charging pile charging technology new energy charging pile development
![[opencv] - Operator (Sobel, canny, Laplacian) learning](/img/24/4e40408b61ec2c714b750b05a534c7.png)
[opencv] - Operator (Sobel, canny, Laplacian) learning

Unity Shader学习(六)实现雷达扫描效果

Application of explosion-proof inclination sensor in safe operation of LNG

MySQL中的时间函数

Day5: PHP simple syntax and usage
随机推荐
Security baseline of network security
Clickhouse learning (II) Clickhouse stand-alone installation
Clickhouse learning (I) Clickhouse?
Cv520 domestic replacement of ci521 13.56MHz contactless reader chip
Chrony time synchronization
Proteus simulation based on 51 MCU ADC0808
Compatible with cc1101/cmt2300-dp4301 sub-1g wireless transceiver chip
Crawling JS encrypted data of playwright actual combat case
Clickhouse learning (III) table engine
C language watch second kill assist repeatedly
Week 2: convolutional neural network basics
Qpalette learning notes
Four pin OLED display based on stm32
Deep learning (1): prediction of bank customer loss
leetcode hot 100(刷题篇9)(301/45/517/407/offer62/MST08.14/)
深度学习(2):图片文字识别
Preparation of SQL judgment statement
Eggjs create application knowledge points
Hc-sr04 use method and routine of ultrasonic ranging module (STM32)
Simulation of four way responder based on 51 single chip microcomputer