当前位置:网站首页>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
边栏推荐
- Anaconda3 安装cv2
- The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
- MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
- MySQL31-MySQL事务日志
- What should the redis cluster solution do? What are the plans?
- 在jupyter NoteBook使用Pytorch进行MNIST实现
- MySQL28-数据库的设计规范
- C miscellaneous lecture continued
- Jar runs with error no main manifest attribute
- 数据库中间件_Mycat总结
猜你喜欢
A necessary soft skill for Software Test Engineers: structured thinking
Super detailed steps for pushing wechat official account H5 messages
14 医疗挂号系统_【阿里云OSS、用户认证与就诊人】
Security design verification of API interface: ticket, signature, timestamp
Preliminary introduction to C miscellaneous lecture document
MySQL28-数据库的设计规范
解决在window中远程连接Linux下的MySQL
Implement sending post request with form data parameter
颜值爆表,推荐两款JSON可视化工具,配合Swagger使用真香
16 medical registration system_ [order by appointment]
随机推荐
Good blog good material record link
ZABBIX introduction and installation
What is the difference between TCP and UDP?
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
UEditor国际化配置,支持中英文切换
Anaconda3 安装cv2
MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?
MySQL27-索引優化與查詢優化
[after reading the series of must know] one of how to realize app automation without programming (preparation)
Simple solution to phpjm encryption problem free phpjm decryption tool
The 32 year old programmer left and was admitted by pinduoduo and foreign enterprises. After drying out his annual salary, he sighed: it's hard to choose
Transactions have four characteristics?
Docker MySQL solves time zone problems
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
使用OVF Tool工具从Esxi 6.7中导出虚拟机
Advantages and disadvantages of evaluation methods
保姆级手把手教你用C语言写三子棋
Super detailed steps for pushing wechat official account H5 messages
Preliminary introduction to C miscellaneous lecture document
Baidu Encyclopedia data crawling and content classification and recognition