当前位置:网站首页>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 .
边栏推荐
- Osgsimplegl3 combined with renderdoc tool
- Compatible with cc1101/cmt2300-dp4301 sub-1g wireless transceiver chip
- OSG advanced sequence
- Detailed steps of installing MySQL 5.7 for windows
- Usage of torch.tensor.to
- Thrift installation manual
- Node: file write data (readfile, WriteFile), two modes: overwrite and increment
- Day15: the file contains the vulnerability range manual (self use file include range)
- C language watch second kill assist repeatedly
- Basic shell operations (Part 2)
猜你喜欢

110 MySQL interview questions and answers (continuously updated)

Reading of false news detection papers (3): semi supervised content-based detection of misinformation via tensor embeddings

Background management system platform of new energy charging pile

Implementation of support vector machine with ml11 sklearn

Proteus simulation based on msp430f2491 (realize water lamp)

Windows 安装 MySQL 5.7详细步骤

Centos7/8 command line installation Oracle11g

DC motor speed regulation system based on 51 single chip microcomputer (use of L298)

MySQL中的时间函数

ROS tutorial (Xavier)
随机推荐
pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies
Brief introduction and use of commonjs import and export and ES6 modules import and export
AES bidirectional encryption and decryption tool
Ar virtual augmentation and reality
Alibaba political commissar system - Chapter III, Alibaba political commissar and cultural docking
集群使用规范
Thrift installation manual
【Transformer】ATS: Adaptive Token Sampling For Efficient Vision Transformers
Node: file write data (readfile, WriteFile), two modes: overwrite and increment
Stm32ff030 replaces domestic MCU dp32g030
Third week weekly report resnet+resnext
Application of explosion-proof inclination sensor in safe operation of LNG
Inclination sensor accuracy calibration test
Inclination sensor is used for long-term monitoring of communication tower and high-voltage tower
Dp1332e multi protocol highly integrated contactless read-write chip
Ga-rpn: recommended area network for guiding anchors
New energy shared charging pile management and operation platform
HC-SR04超声波测距模块使用方法和例程(STM32)
Windows 安装 MySQL 5.7详细步骤
What if official account does not support markdown format file preparation?