当前位置:网站首页>Function default parameters, function placeholder parameters, function overloading and precautions
Function default parameters, function placeholder parameters, function overloading and precautions
2022-07-05 22:35:00 【Dark purple Qiao song (- _^)】
stay C++ in , The parameters in the function's parameter list can have default values
grammar : return type Function name ( Parameters = The default value is ){ }
#include<iostream>
usingnamespace std;
// If we pass in parameters ourselves , Just use your own data , without , Then use the default value
int func(inta, intb=20, intc=30)
{
returna + b + c;
}
// matters needing attention
//1. If a location already has default parameters , So back from this position , There must be default values from left to right
//int func2(int a = 10, int b, int c) That's the wrong way to write it !
//{
// return a + b + c;
//}
//2. If the function declaration has default arguments , Function implementations cannot have default parameters
// Declaration and implementation can only have one default parameter
//int func3(int a, int b = 20, int c = 30); That's the wrong way to write it !
//int func3(int a, int b = 20, int c = 30)
//{
// return a + b + c;
//}
int main()
{
cout << func(10,30) << endl;
system("pause");
return 0;
}
2. Function space occupying parameter
C++ There can be space occupying parameters in the formal parameter list of functions in , For space occupying , This position must be filled when the function is called
grammar : return type Function name ( data type ){ }
#include<iostream>
usingnamespace std;
// Placeholder parameters
// The placeholder parameter can also have a default parameter // If there is a default value, it is useless
void func(inta, int)
{
cout <<"helloworld"<< endl;
}
int main()
{
func(10,34);
system("pause");
return 0;
}
3. function overloading
effect : Function names can be the same , Improve reusability
Function overload satisfies the condition :
1. Under the same scope
2. Same function name
3. Function parameter Different types perhaps The number is different. perhaps Different order
Be careful : The return value of a function cannot be used as a condition for overloading a function
#include<iostream>
usingnamespace std;
void func()
{
cout <<"helloworld"<< endl;
}
void func(inta)
{
cout <<a<< endl;
}
void func(doubleb)
{
cout <<b<< endl;
}
void func(doubleb, intc)
{
cout <<b<<" "<<c<< endl;
}
int main()
{
func();
func(19);
func(6.34);
func(3.14, 3);
system("pause");
return 0;
}
4. Function overload considerations
Reference as overload condition
Function overload encountered function default parameter
#include<iostream>
usingnamespace std;
void func(int& a)//int &a=10; illegal
{
cout <<"int &a call "<<endl;
}
void func(constint& a)//const int &a=10 legal
{
cout <<"const int &a call "<< endl;
}
void func2(inta,intb=10)
{
cout <<"func2 int a,int b=10 Call to "<< endl;
}
void func2(inta)
{
cout <<"func2 int a Call to "<< endl;
}
int main()
{
int a = 10;
func(a);// Adjust the first
func(10);// Tune the second
func2(10);// When a function overload encounters a default parameter , There is ambiguity , Will report a mistake , The third and fourth functions do not know which one to call
}
边栏推荐
- Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology
- a-tree 树的全部展开和收起
- Lesson 1: serpentine matrix
- 我对新中台模型的一些经验思考总结
- 实战:fabric 用户证书吊销操作流程
- Platformio create libopencm3 + FreeRTOS project
- Character conversion PTA
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- Go language learning tutorial (XV)
- Record several frequently asked questions (202207)
猜你喜欢

Leetcode simple question: find the nearest point with the same X or Y coordinate

Metaverse ape ape community was invited to attend the 2022 Guangdong Hong Kong Macao Great Bay metauniverse and Web3.0 theme summit to share the evolution of ape community civilization from technology

Distributed resource management and task scheduling framework yarn

How can Bluetooth in notebook computer be used to connect headphones

Practice: fabric user certificate revocation operation process

Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)

Kubernetes Administrator certification (CKA) exam notes (IV)

南京:全面启用商品房买卖电子合同

opencv 判断点在多边形内外

700. Search in a Binary Search Tree. Sol
随机推荐
Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
The introduction to go language is very simple: String
APK加固技术的演变,APK加固技术和不足之处
Matlab draws a cute fat doll
Draw a red lantern with MATLAB
二叉树(三)——堆排序优化、TOP K问题
Metaverse Ape获Negentropy Capital种子轮融资350万美元
Three "factions" in the metauniverse
等到产业互联网时代真正发展成熟,我们将会看待一系列的新产业巨头的出现
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
Business introduction of Zhengda international futures company
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
南京:全面启用商品房买卖电子合同
Platform bus
700. Search in a Binary Search Tree. Sol
What if the files on the USB flash disk cannot be deleted? Win11 unable to delete U disk file solution tutorial
Damn, window in ie open()
Binary tree (II) -- code implementation of heap
a-tree 树的全部展开和收起