当前位置:网站首页>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
}
边栏推荐
- FBO and RBO disappeared in webgpu
- Oracle triggers
- Technology cloud report: how many hurdles does the computing power network need to cross?
- Promql demo service
- I closed the open source project alinesno cloud service
- Platform bus
- 344. Reverse String. Sol
- Distributed resource management and task scheduling framework yarn
- C language - structural basis
- 如何创建线程
猜你喜欢
随机推荐
The code generator has deoptimised the styling of xx/typescript. js as it exceeds the max of 500kb
Opencv judgment points are inside and outside the polygon
FBO and RBO disappeared in webgpu
2022软件测试工程师涨薪攻略,3年如何达到30K
Lesson 1: serpentine matrix
Oracle views the data size of a table
[error record] file search strategy in groovy project (src/main/groovy/script.groovy needs to be used in the main function | groovy script directly uses the relative path of code)
Oracle is sorted by creation time. If the creation time is empty, the record is placed last
Solve the problem of "no input file specified" when ThinkPHP starts
Nanjing: full use of electronic contracts for commercial housing sales
TCC of distributed solutions
Distributed resource management and task scheduling framework yarn
Request preview display of binary data and Base64 format data
Text组件新增内容通过tag_config设置前景色、背景色
opencv 判断点在多边形内外
How to quickly experience oneos
My experience and summary of the new Zhongtai model
Solutions for unexplained downtime of MySQL services
Unity Max and min constraint adjustment
Three "factions" in the metauniverse