当前位置:网站首页>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
边栏推荐
- [C language] deeply analyze the underlying principle of data storage
- MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
- Use of dataset of pytorch
- MySQL ERROR 1040: Too many connections
- MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
- MySQL的存储引擎
- MySQL transaction log
- Introduction tutorial of typescript (dark horse programmer of station B)
- First blog
- MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
猜你喜欢
Nanny hand-in-hand teaches you to write Gobang in C language
软件测试工程师必备之软技能:结构化思维
MySQL 29 other database tuning strategies
MySQL Real Time Optimization Master 04 discute de ce qu'est binlog en mettant à jour le processus d'exécution des déclarations dans le moteur de stockage InnoDB.
实现以form-data参数发送post请求
Redis集群方案应该怎么做?都有哪些方案?
jar运行报错no main manifest attribute
Ueeditor internationalization configuration, supporting Chinese and English switching
The appearance is popular. Two JSON visualization tools are recommended for use with swagger. It's really fragrant
Routes and resources of AI
随机推荐
A necessary soft skill for Software Test Engineers: structured thinking
15 医疗挂号系统_【预约挂号】
MySQL combat optimization expert 04 uses the execution process of update statements in the InnoDB storage engine to talk about what binlog is?
Implement sending post request with form data parameter
Solution to the problem of cross domain inaccessibility of Chrome browser
Vscode common instructions
What is the difference between TCP and UDP?
C miscellaneous shallow copy and deep copy
MySQL的存储引擎
Mysql36 database backup and recovery
text 文本数据增强方法 data argumentation
MySQL28-数据库的设计规范
Bytetrack: multi object tracking by associating every detection box paper reading notes ()
The underlying logical architecture of MySQL
使用OVF Tool工具从Esxi 6.7中导出虚拟机
MySQL storage engine
Transactions have four characteristics?
If someone asks you about the consistency of database cache, send this article directly to him
解决在window中远程连接Linux下的MySQL
Use xtrabackup for MySQL database physical backup