当前位置:网站首页>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
}
边栏推荐
- Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
- Performance testing of software testing
- How to reverse a string fromCharCode? - How to reverse String. fromCharCode?
- Pinctrl subsystem and GPIO subsystem
- Metaverse Ape上线倒计时,推荐活动火爆进行
- A substring with a length of three and different characters in the leetcode simple question
- Starting from 1.5, build a micro Service Framework -- log tracking traceid
- [groovy] mop meta object protocol and meta programming (Introduction to groovyobject interface | introduction to metaclass | implementation of class methods using groovyobject invokemethod)
- 119. Pascal‘s Triangle II. Sol
- 70. Climbing Stairs. Sol
猜你喜欢
Win11运行cmd提示“请求的操作需要提升”的解决方法
Starting from 1.5, build a micro Service Framework -- log tracking traceid
The simple problem of leetcode is to split a string into several groups of length K
Technology cloud report: how many hurdles does the computing power network need to cross?
a-tree 树的全部展开和收起
The countdown to the launch of metaverse ape is hot
南京:全面启用商品房买卖电子合同
A trip to Suzhou during the Dragon Boat Festival holiday
Distance entre les points et les lignes
Metasploit (MSF) uses MS17_ 010 (eternal blue) encoding:: undefined conversionerror problem
随机推荐
Editor extensions in unity
Metasploit(msf)利用ms17_010(永恒之蓝)出现Encoding::UndefinedConversionError问题
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
2022-07-05: given an array, you want to query the maximum value in any range at any time. If it is only established according to the initial array and has not been modified in the future, the RMQ meth
第一讲:蛇形矩阵
Ieventsystemhandler event interface
How to quickly experience oneos
FBO and RBO disappeared in webgpu
抖音__ac_signature
Search: Future Vision (moving sword)
我把开源项目alinesno-cloud-service关闭了
Leetcode simple question: check whether each row and column contain all integers
Starting from 1.5, build a micro Service Framework -- log tracking traceid
Oracle views the data size of a table
Double pointer of linked list (fast and slow pointer, sequential pointer, head and tail pointer)
MCU case -int0 and INT1 interrupt count
Double pointeur de liste liée (pointeur rapide et lent, pointeur séquentiel, pointeur de tête et de queue)
Lesson 1: serpentine matrix
[secretly kill little buddy pytorch20 days] - [Day2] - [example of picture data modeling process]
90后测试员:“入职阿里,这一次,我决定不在跳槽了”