当前位置:网站首页>02-2. Default parameters, function overloading, reference, implicit type conversion, about error reporting
02-2. Default parameters, function overloading, reference, implicit type conversion, about error reporting
2022-07-19 11:31:00 【The streets of Leningrad】
Preface : This article is mainly used for personal review , Pursue simplicity , Thank you for your reference 、 Communication and handling , It may be revised and improved in the future .
Because it's a personal review , There will be partial compression and omission .
One 、 Default parameters
The default parameter is When declaring or defining a function Specify a... For the arguments of the function The default value is . When the function is called , If no argument is specified, the default value is taken , Otherwise, the specified argument is used .
There can be multiple default parameters , But the default parameter cannot appear in the declaration and definition at the same time
The default parameters are divided into full default parameters and semi default parameters ( Some parameters are defaulted ), Among them, the semi default parameters must default from right to left , And it's continuous ( Because when giving value , It's from right to left ).
Default parameter function : Make calls more flexible
The default value must be a constant or a global variable ( It's a constant ), Variables cannot be compiled
Two 、 function overloading
function overloading : It's a special case of a function ,C++ It is allowed to declare several functions of the same name with similar functions in the same scope , The formal parameter list of these functions with the same name ( Number of parameters or type or The order ) Must be different , It is often used to deal with the implementation of similar functions and different data types
function overloading : Different parameter types, different number of parameters or different order of parameters
The following situations constitute overloads , first add Can run successfully , But the second one cannot
void add(int x = 0,int y = 0,int z = 0)
{
return x+y+z;
}
void add(int x = 0, int y = 0)
{
return x+y;
}
int main()
{
add(1,2,3);
add(1,2);
return 0;
}If you want to match long integer , Can be in 2 Add one at the end l
3、 ... and 、 quote
A reference is not a new definition of a variable , Instead, it takes an alias for an existing variable , The compiler doesn't open up memory for reference variables , It shares the same memory space with the variables it references .
Reference functions that can be used to write exchange variables .
References must be initialized , Whether it can be referenced should pay attention to the size of access rights . Permission cannot be enlarged , Permissions can be reduced
Properties of references :
1. A variable can have multiple aliases .
2. Reference must be initialized at definition time ( Also cannot write empty NULL). A variable can have multiple references . Reference once an entity is referenced , No more references to other entities . Once quoted , Can't change easily
3. Reference once an entity is referenced , You can't refer to other entities
const int a = 10;// read-only , It belongs to the category that cannot be modified
int& ra = a;// You can't do that ,rayinyonga It belongs to permission amplification , So no . Because doing so enlarges the permission
// Change to const int &ra = a; This can
int b = 10;
int& rb = b;// Sure
const int& crb = b;// Reduction of permissions , Sure
int c = 10;
double d = 1.11;
d = c;// Sure , This is implicit type conversion
Four 、 Implicit type conversion
int c = 10;
double d = 1.11;
d = c;// Sure , This is implicit type conversion Not directly c to d, Instead, a temporary variable is generated , Then give the temporary variable to d
int c = 10;
double d = 1.11;
d = c;// Sure , This is implicit type conversion
double& rc = c;// Can not be
const double& brc = c;// Sure
// here rc、brc The reference is not c( That is not c Another name for ), It is c The resulting temporary variables , Temporary variables are constant , So add const.( The temporary variable type is double)
Mandatory type conversion is basically the same as implicit type conversion , Because they all produce temporary variables , They do not change c Of itself , They all generate temporary variables . And their conversion will not affect c( Whether it's c What kind of ).
Referenced scenes :1. Make parameters 2. Do the return value ( In a special situation )
Typical parameters :swap In exchange for
void swap(int& left, int& right)
{
` int temp = left;
left = right;
right = temp;
}Do the return value :
int& add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int& ret = add(1,2);// This can't be made up
return 0;
}At this time, the type of temporary variable is int&. The returned value is c A copy of the , Pass the reference and return c References to
actually , If there is a function scope , The return variable does not exist , Cannot return... By reference
Passing a value back will also create a temporary variable for the value , for example return c; Will create a c The temporary variable of , Then return the temporary variable . So how to prove that it produces temporary variables ?
int add(int a, int b)
{
int c = a + b;
return c;
}
int main()
{
int& ret = add(1,2);// This can't be made up
const int& ret = add(1,2);// This can
return 0;
}Because temporary variables are constant , So I added const To edit
When the parameter and return value are relatively large variables , Pass reference, pass parameter and pass reference as return value can also improve efficiency , As long as the conditions are met , Try to use reference parameters as return values , Referencing and passing parameters can also avoid some deep copies
5、 ... and 、 About cross-border error reporting
About cross-border error reporting :Linux The next check is quite strict .
Crossing the boundary is not necessarily wrong , Cross border reading ( That's printing ......) Basically no error , Because the compilation cannot be checked .
Write across the border ( namely a[12] = 0) There may be a mistake .
The system uses the method of spot check about crossing the boundary , Usually, the check bit is set in the next two positions of the array . When the function of this array ends , It will check whether the set check bit has been changed , If it is changed, it will report an error , Just like checking drunk driving in reality .
边栏推荐
- Category imbalance in classification tasks
- 03-2、
- Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
- AT5147-[AGC036D]Negative Cycle【dp,模型转换】
- Conversion of unity3d model center point (source code)
- Introduction to replacement technology of SAP ABAP CDs view view
- MySQL autoincrement ID, UUID and snowflake ID
- QT two overloaded qlistwidget control objects implement selectitem drag drag
- Huawei machine test: Message decompression
- Cmake常用命令(五)
猜你喜欢

早期单片机加密的一些方法 【评论区领取资料】

mysql show processlist 详解

Performance optimization @contented to reduce pseudo sharing

Hot discussion: my husband is 34 years old this year and wants to read a doctoral degree. What should I do in the future to do scientific research?

Four methods of traversing key value in map

每日刷题记录 (二十六)

Déléguer un chargeur tel qu'un parent

Un modèle de détection par défaut basé sur le réseau neuronal évolutif rapide dans le contrôle de la qualité des produits - lire les notes

Unity3d read mpu9250 example source code

Tire Defect Detection Using Fully Convolutional Network-论文阅读笔记
随机推荐
ZABBIX proxy server configuration
Dynamic memory allocation problem
Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
The case has been solved --- no matter how to change the code from the logic of MQ consumption, it will not take effect
Hot discussion: my husband is 34 years old this year and wants to read a doctoral degree. What should I do in the future to do scientific research?
Leetcode 1304. 和为零的 N 个不同整数
Powercli script performance optimization
Cmake常用命令(五)
微服务上线规范
Deep Learning for Generic Object Detection: A Survey-论文阅读笔记
Some methods of early MCU encryption [get data in the comment area]
Loj#2324-「清华集训 2017」小 Y 和二叉树
Chapter 1 of creating virtual machine (vmvare virtual machine)
Unity高版本退回低版本报错问题
TCP拥塞控制详解 | 7. 超越TCP
常用getshell工具的下载
Model comparison of material inventory management between sap ECC and s4hana material
TS solves the problem that the type file of the imported plug-in does not exist
Dream CMS Front Office Search SQL Injection
Automated graphical interface library pyautogui