当前位置:网站首页>Quoted popular explanation
Quoted popular explanation
2022-07-27 05:09:00 【Meme_ xp】
title : quote
1. quote : Give the variable another name .
2. grammar : data type & Alias = Original name .
Code demonstration :
int a=10;
int &b=a;
b=20;
Here a It will become 20, Because after quotation ,b and a All operate on the same piece of memory .
2.1 Notes on quotation :
2.1.1 References must be initialized
int a=10,b=20;
int &c;// Error code , References must be initialized
2.1.2 The reference cannot be modified after initialization
int a=10,b=20;
int &c=a;
c=b;
// This position is the assignment operation , It's not about changing references
int &c=b;
// This sentence is the error code , Because the reference is changed
Why references cannot be changed , We will explain it step by step later
3. The main function of quotation : In general function calls , The system passes the value of the argument to the formal parameter by means of value transfer , The system will open up space in memory to store formal parameter variables , And copy the value of the argument to the formal parameter , And use reference as function parameter , Just pass the alias of the function argument , You don't need to spend time opening up space in memory to store formal parameters , Using a reference , It can improve the time efficiency of the function , And save memory space .
4. Reference as the return value of the function ( Fallible part ):
A reference can exist as the return value of a function , But we cannot return local variable references . Why? ?
Example :
The wrong way to write :
int &test01()
{
int a=10;
return a;
}
Here we refer to local variables , For quotes , References to functions , Equivalent to the alias of the return variable , And the life of local variables will die as the function call ends ( In the stack area , Create when calling , Destroy after calling ), How can I quote , Popularly speaking , It's all gone , So who are you looking for . But some smart compilers will keep the answers you need for you. There will be no errors but warnings . Don't take chances just because of warning !
Correct writing :
int &test02()
{
static int a=10;
return a;
}
This writing is correct , This variable a Stored in the global area .
5. The essence of quotation :
We left a question above . Why references cannot be changed . In fact, the essence of quotation is c++ Inside is a pointer constant . In my 《 Constants and pointers 》 This article said , Pointer constants cannot change the direction of the pointer , So we cannot change the reference .
int &a=c;
// The essence is :int * const a=&c
6. const reference :
6.1 effect : Literally, it's from this point of view , Think of this reference as a constant , No modification allowed . Often quoted is mainly used to modify formal parameters , Prevent misoperation . Usually when we pass an address into a function , We don't want the parameters in the main function to be changed , So we add const Modified parameter , Prevent changing arguments .
6.2 A special case : As we mentioned earlier , The type of reference must be consistent with the referenced object , However, there is another exception that allows arbitrary expressions to be used as initial values when initializing constant references , As long as the result can be converted into the type of reference . You can even allow a constant reference to bind a very large number of objects 、 Face value , It could also be an expression
int i=42;//
const int &r1=i;// correct , Allows you to const int & Bound in normal int The above object
const int &r2=42;// correct , It is allowed to reference a constant
const int &r3=r1*2;// correct , It is allowed to reference an expression
inr &r4=42;// error ,r4 Not a very large number of references
To understand this situation, we need to figure out what happens when a constant reference is bound to another type :
double pi=3.14;
const int &ri=pi;
there ri It's a int The number of types , however pi It is a double precision floating-point number, not an integer . therefore , To ensure that ri Bind an integer , The compiler changes the above code into the following form :
const int temp=pi;// Give Way double Create a temporary int Type constant
const int &ri=temp;// Give Way ri Bind the temp
This is normal , Notice if we don't want that const, The code will look like this :
const int temp=pi;
int &ri=pi;
We said that above :
int &a=c;
// The essence is :int * const a=&c
well , We now make the corresponding replacement , That's what it looks like :
const int temp=pi;// Give Way double Create a temporary int Type constant
int * const ri=&temp;// Give Way ri Bind the temp
We found a problem , If we don't need the front one const, What we get is a temp The address of , however !temp Is a temporary variable , It will be destroyed ! After destruction int * const ri=&temp This sentence will have no meaning !
Now we add const It becomes const int * const ri=&temp you 're right , He was released , But we can understand that its value has been saved in the global area as a constant before release . Even if the temp No error will be reported after release .
Most of the above are for your own understanding and learning c++ The notes . If you have any questions, you can ask them in the comment area or write to me personally , If you have any problems, please correct them .
边栏推荐
- How idea creates a groovy project (explain in detail with pictures and texts)
- 事件(event)
- Final Cut Pro Chinese tutorial (2) understanding of material window
- 老子云携手福昕鲲鹏,首次实现3D OFD三维版式文档的重大突破
- Invert a Binary Tree
- Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
- Sub database and sub table
- 项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
- [error reporting] cannot read property 'parsecomponent' of undefined
- Three paradigms, constraints, some keyword differences,
猜你喜欢

strlen和sizeof的区别

JVM上篇:内存与垃圾回收篇十四--垃圾回收器

Introduction to dynamic memory functions (malloc free calloc realloc)

1、 MySQL Foundation

《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译

Event

【Acwing】第61场周赛 题解
![[error reporting]: cannot read properties of undefined (reading 'prototype')](/img/e4/528480610b9eed6028d7b3b87571e2.png)
[error reporting]: cannot read properties of undefined (reading 'prototype')

Install pyGame

一、MySQL基础
随机推荐
Acticiti中startProcessInstanceByKey方法在variable表中的如何存储
How do I reset Photoshop preferences? PS method of resetting preferences
static和final关键字 学习 demo练习
Explanation of index failure principle and its common situations
【搜索】DFS之连通性模型 + 搜索顺序
QT menu bar, toolbar and status bar
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
Complete Binary Tree
feign调用丢失请求头问题解决及原理分析
How to sinicize the JMeter interface?
[search] connectivity model of DFS + search order
Static and final keyword learning demo exercise
Constraints of MySQL table
《Robust and Precise Vehicle Localization based on Multi-sensor Fusionin Diverse City Scenes》翻译
Final Cut Pro Chinese tutorial (2) understanding of material window
Introduction to Kali system ARP (network disconnection sniffing password packet capturing)
How to import PS style? Photoshop style import tutorial
测试基础5
Row, table, page, share, exclusive, pessimistic, optimistic, deadlock
Solution: read the files with different names in the two folders and deal with the files with different mappings