当前位置:网站首页>Just remember Balabala
Just remember Balabala
2022-07-06 10:30:00 【Don't understand anything】
const References to
Usually : The type of reference must strictly match the object type it is bound to
int a = 100;
int &val = a;
But constant references don't need .
double i = 14.42;
const int& i0 = i;
cout << i0;
The final output is 14;
in other words , The constant reference will carry out type conversion on the referenced object whose type does not match .
Here i0 Quoted a int Object of type , however i It's a double floating point number . So to ensure that i0 Bound to an integer , The compiler changes the above code into the following form :
const int temp = i; // Use variables to realize type conversion
const int &i0 = temp; // Reuse i0 For temporary quantity ( At this time, the type is matched ) Binding
It's nice .
in addition , At this time, constant references , In fact, it seems not to be a constant in the strict sense , Although but , We still can't directly deal with const References to objects , Assign a value , Or other operations that try to change constants . however , You can change the value of constants in other ways
#include<iostream>
using namespace std;
int main() {
int val = 100;
const int &cval = val;
cout << cval << endl;
int& ival = val;
++ival;
cout << cval;
}
The output is
100
101
Or directly to the original object val Assign a value , It will change too. const The value of the reference of the object .
Add : Usually , References cannot be assigned with literal values , But constant references are ok
const int &val = 42;
cout << val;
The result is 42
in other words , Objects cannot be changed by constant references , It doesn't matter whether it's bound to a literal or an object , Anyway, we can't make changes, so there is no meaningless change .
Pointers and const
const double pi = 3.14;// Initialize double constant pi
const double* cptr = π// You can only use a pointer to a constant to store the address of a constant object
double a = 100;
cptr = &a;
cout << *cptr; // Output 100;
++ *cptr;// Constant cannot be assigned
The type of the pointer must be consistent with the type of the object it refers to . But there are two exceptions , The first exception is Allow a pointer to a constant to point to a non constant object .
Look at the code above , We found that , pointer to const , It only requires that the value of the object cannot be changed through this pointer , We can even change the pointer directly , Point to a very large number of objects , There is no binding relationship between the two .
This is the picture I drew myself , My own understanding , I don't know if there are any mistakes ,const The address of the constant must be stored in const * Type in the , however const * You can also store a very large number of addresses . But no matter whose address it is stored , Can't pass the dereference character “*”, Yes Object . and const Same reference , Although it can't be directly through the pointer pair Object , But there are other ways to change the object , It doesn't matter .
there const Just saying : This pointer points to a constant , This constant cannot be arbitrarily changed through this pointer . And what to say next const Pointers are very different .
In the book : A pointer or reference to a constant , Just pointers or references “ Be opinionated ” only , They feel that they point to constants , So consciously do not change the value of the object .
const The pointer
Actually, the above , Our focus is on the type . So we can change the direction of the pointer . What to say next const The pointer , Is to set the pointer itself as a constant . Constant pointers are the same as constants , Must be initialized , And after initialization , The address stored in the pointer can no longer be changed .
#include<iostream>
using namespace std;
int main() {
int val = 100;
int* const cptr = &val; // hold * Put it before the keyword to indicate that the pointer is a constant
// This writing form implies a layer of meaning
// That is, the constant is the value of the pointer itself ( The address of the object pointed to )
// Instead of pointing to the object
// The object pointed to can be modified by the dereference character .
const double pi = 3.14;
// The first one here const This ensures that this constant cannot be modified by a pointer
// the second const To ensure the , The address of this pointer , Already bound to this pointer , Can't change .
const double* const pip = π
//pip = &val; illegal , because pip Is a constant pointer , After being initialized , You cannot change the value
//*pip = 1111; illegal , It points to a double constant , Cannot be modified by this pointer , The value of the object he points to .
int* const cptr1 = &val;
*cptr1 = 100;
cout << *cptr1;// Output 100, The type here int
// Means that after dereferencing, you get a int Variable of type , You can change the value
// If it is const int , Then the value cannot be modified
}
I feel this kind of definition statement , Look from right to left , It's a good choice .
top floor const
As mentioned earlier , The pointer itself is an object , It can point to another object . So the pointer itself is a constant , And whether the pointer refers to a constant is two independent problems .
Bottom const: Indicates that the object pointed to is a constant , Related to composite types such as pointers and references .
top floor const: Indicates that the pointer itself is a constant , top floor const Any object can be represented as a constant , For any data type .
What's more special is : The pointer can be either the bottom layer const, It can also be the top floor const
int const a = 100;
cout << a;
// I found that more than pointers can be used after types const, Other data types can also .
Used to declare references to const It's all at the bottom const, Taking the address of constant object is also const
const int ci =100;
&ci: Bottom const
int const a = 100; int b = a; const int* c = &a;// top floor const You can give it to the bottom const initialization . //int* const d = c;// Bottom const Not to the top const initialization ( Copy ), Because because c Itself is the bottom layer const, It is itself a variable // however d It's the top floor const, A constant . So variables cannot be copied to a constant . // Let's do another example , Examples cited const int& bval = b;// References are all at the bottom const const int* const p = &bval; //int* const p2 = &bval; p2 It's a top floor const, however bval It's a ground floor const, Cannot perform
边栏推荐
- 安装OpenCV时遇到的几种错误
- Bytetrack: multi object tracking by associating every detection box paper reading notes ()
- Security design verification of API interface: ticket, signature, timestamp
- 颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
- Adaptive Bezier curve network for real-time end-to-end text recognition
- MySQL实战优化高手08 生产经验:在数据库的压测过程中,如何360度无死角观察机器性能?
- The programming ranking list came out in February. Is the result as you expected?
- Time complexity (see which sentence is executed the most times)
- How to build an interface automation testing framework?
- MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计
猜你喜欢
[C language] deeply analyze the underlying principle of data storage
South China Technology stack cnn+bilstm+attention
How to build an interface automation testing framework?
Mysql30 transaction Basics
MySQL34-其他数据库日志
MySQL35-主从复制
MySQL33-多版本并发控制
MySQL28-数据库的设计规范
Complete web login process through filter
MySQL27-索引优化与查询优化
随机推荐
Simple solution to phpjm encryption problem free phpjm decryption tool
Windchill configure remote Oracle database connection
该不会还有人不懂用C语言写扫雷游戏吧
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
Security design verification of API interface: ticket, signature, timestamp
Mexican SQL manual injection vulnerability test (mongodb database) problem solution
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
MySQL實戰優化高手08 生產經驗:在數據庫的壓測過程中,如何360度無死角觀察機器性能?
Notes of Dr. Carolyn ROS é's social networking speech
13 医疗挂号系统_【 微信登录】
Time in TCP state_ The role of wait?
【C语言】深度剖析数据存储的底层原理
基于Pytorch肺部感染识别案例(采用ResNet网络结构)
Pytorch RNN actual combat case_ MNIST handwriting font recognition
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
What is the current situation of the game industry in the Internet world?
MySQL33-多版本并发控制
Super detailed steps to implement Wechat public number H5 Message push
Export virtual machines from esxi 6.7 using OVF tool