当前位置:网站首页>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
}
边栏推荐
- Sub total of Pico development
- Evolution of APK reinforcement technology, APK reinforcement technology and shortcomings
- 2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
- Oracle hint understanding
- When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
- Technology cloud report won the special contribution award for the 10th anniversary of 2013-2022 of the "cloud Ding Award" of the global cloud computing conference
- [groovy] mop meta object protocol and meta programming (execute groovy methods through metamethod invoke)
- Pl/sql basic syntax
- 我把开源项目alinesno-cloud-service关闭了
猜你喜欢
A substring with a length of three and different characters in the leetcode simple question
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
The statistics of leetcode simple question is the public string that has appeared once
Distance from point to line intersection and included angle of line
Common interview questions of redis factory
Navigation day answer applet: preliminary competition of navigation knowledge competition
[groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
Postman核心功能解析-参数化和测试报告
Oracle hint understanding
MCU case -int0 and INT1 interrupt count
随机推荐
二叉树(二)——堆的代码实现
513. Find the value in the lower left corner of the tree
Post-90s tester: "after joining Ali, this time, I decided not to change jobs."
Record several frequently asked questions (202207)
2022 Software Test Engineer salary increase strategy, how to reach 30K in three years
TCC of distributed solutions
了解 Android Kotlin 中 DataStore 的基本概念以及为什么应该停止在 Android 中使用 SharedPreferences
How to create a thread
Qtquick3d real time reflection
Thinkphp5.1 cross domain problem solving
Platformio create libopencm3 + FreeRTOS project
344. Reverse String. Sol
Three "factions" in the metauniverse
When the industrial Internet era is truly mature, we will look at the emergence of a series of new industrial giants
How can Bluetooth in notebook computer be used to connect headphones
Binary tree (III) -- heap sort optimization, top k problem
Win11运行cmd提示“请求的操作需要提升”的解决方法
70. Climbing Stairs. Sol
Alternating merging strings of leetcode simple questions
Go language learning tutorial (XV)