当前位置:网站首页>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
}
边栏推荐
- Common interview questions of JVM manufacturers
- The code generator has deoptimised the styling of xx/typescript.js as it exceeds the max of 500kb
- Nanjing: full use of electronic contracts for commercial housing sales
- 點到直線的距離直線的交點及夾角
- Go语言学习教程(十五)
- Performance testing of software testing
- FBO and RBO disappeared in webgpu
- 记录几个常见问题(202207)
- 数据泄露怎么办?'华生·K'7招消灭安全威胁
- Win11 runs CMD to prompt the solution of "the requested operation needs to be promoted"
猜你喜欢
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Usage Summary of scriptable object in unity
Unique occurrence times of leetcode simple questions
Matlab draws a cute fat doll
Promql demo service
Damn, window in ie open()
How can Bluetooth in notebook computer be used to connect headphones
MySQL actual combat 45 lecture learning (I)
Pl/sql basic syntax
Oracle advanced query
随机推荐
C language - structural basis
记录几个常见问题(202207)
从 1.5 开始搭建一个微服务框架——日志追踪 traceId
请求二进制数据和base64格式数据的预览显示
What about data leakage? " Watson k'7 moves to eliminate security threats
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Metaverse ape received $3.5 million in seed round financing from negentropy capital
Go language learning tutorial (XV)
New 3D particle function in QT 6.3
Calculation method of boundary IOU
Leetcode simple question ring and rod
Alternating merging strings of leetcode simple questions
Oracle hint understanding
boundary IoU 的计算方式
Common interview questions of redis factory
The countdown to the launch of metaverse ape is hot
Understand the basic concept of datastore in Android kotlin and why SharedPreferences should be stopped in Android
Postman核心功能解析-参数化和测试报告
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb