当前位置:网站首页>On the corners of const
On the corners of const
2022-06-21 10:36:00 【Longcheng deficit】
About const In every corner of the world
const Keyword provides auxiliary reference information for program robustness and compilation optimization .
const It is mainly interpreted and used by the compiler , It's a static concept . That is to say , When we pass a variable const When the description is constant , Subsequent modifications to this variable , Will be checked by the compiler , Then an error .
however , Once the inspection is passed , In actual operation , It is no different from ordinary variables . such as , If we get the address of the constant by other means , You can also change its value at run time . In the memory , It is still represented as a common data variable .
const There are many descriptive articles on the Internet . Authoritative interpretation , Personally, I think it should be the standard description . The compiler shall comply with the standard requirements , For the const Carry out inspection and treatment .
const You can modify variables and functions .
Modifying variables , It can be a built-in type , It can also be a custom type .
The built-in type can be a normal variable , It can also be a pointer variable , It can also be a reference .
Modify function , You can modify the return value of a function , The parameters of the function , The function itself .
The function can be C function , It can be C++ The function in ,C++ Including static functions and member functions .
Modification of the return value and parameters of a function , Personal understanding is based on the modification of variables , Many places can be understood by referring to the modification of variables .
Modification of the function itself , Mainly for C++ in , In fact, it is decorated this The pointer . such , We understand const Member variables cannot be modified in a function , Just a simple . otherwise , Simply memorize by rote , The effect is not good .
Here we mainly look at the modification of the function return value . To elicit questions , Let's start with a few simple examples :
Decoration of common built-in types , such as
int const cint1 = 1;
const int cint2 = 2;
The compiler will require an initial value when defining , And elsewhere in the code , You can only quote , Can't change value , Otherwise it will go wrong . Same as before , These are all compilation checks .
For the above two ways , Compilers seem to be all about const Variable to handle , There is no difference . This point , You'll see that in the back . Let me first mention .
however , For pointer variables , The above two ways of writing are different
int * const cpi1 = &xx;
const int * cpi2;
Assign values when the first one needs to be defined , Otherwise the compiler will tell you constants cpi1 Not initialized . therefore , here cpi1 Is a constant , Is not a variable , The point should be clear from the beginning .
The second one can be compiled through , explain cpi2 It's a variable , Is not a constant . however , If you try to assign a value to the content it points to , Error will be reported in compilation , Tips *cpi2 Is a constant , That is, the content pointed to by the pointer is a constant .
Because this article is to study const In every corner of the world , So let's see const Put it in the middle :
int const * cpi3;
actually , This kind of writing is rare , It's a little nondescript , The compiler presses cpi2 Handle it the same way .
below , Let's see. const Decorating functions .
For common built-in types , We can rely on intuition , Write the following example :
const int func();
however , Similar to variables , There are also the following possibilities :
int const func();
As I said before , Modification of the function itself , Is placed after the function , That is, something like this ,int func() const, So here are all decorated return values .
In order to see the difference between the above two ways , We make mistakes , Let the compiler tell us where the error is , Through error messages , Further judge the difference .
How to make mistakes ?
There is a way , We declare a function pointer , Then define a function , Let the function pointer point to the function , If the two definitions are inconsistent , Error will be reported when compiling , Here is the starting point .
typedef const int (*pfunc)();
const int getVal2() {return 123;}
pfunc xxx;
xxx = getVal2;
Because the above types are consistent , So compile through , We will declare that const Get rid of , have a look
typedef int (*pfunc)();
Compiler error
invalid conversion from ‘const int (*)()’ to ‘pfunc {aka int (*)()}’
We will define the const Move , have a look
The compiler reported the same error
invalid conversion from ‘const int (*)()’ to ‘pfunc {aka int (*)()}’
This shows two ways of writing , There is no difference in the compiler's view .
Okay , We replace the basic built-in type with a pointer , See what happens
const int * getVal2() {return &g_var;}
The compiler error is as follows :
invalid conversion from ‘const int* (*)()’ to ‘pfunc {aka int (*)()}’
take const Change the position ,
int const * getVal2() {return &g_var;}
invalid conversion from ‘const int* (*)()’ to ‘pfunc {aka int (*)()}’
Errors do not change , This is consistent with the pointer variable situation described above
Let's see ,const Put it in * What happened later
int * const getVal2() {return &g_var;}
Compiler error changed ,
invalid conversion from ‘int* const (*)()’ to ‘pfunc {aka int (*)()}’
These three ( There are essentially two kinds of ) What are the differences in writing ?
For the first and second , That is to say const In the previous case ,
int * presult = getVal2();
This writing method will report compilation errors ,invalid conversion from ‘const int*’ to ‘int*’
It returns a pointer , The data pointed to by the pointer is a constant , Do not modify , The lvalue also needs to be a pointer of the same type .
For the third way of writing , The above lvalue compilation will not be a problem .const Decorated function name , The function name itself is not modifiable , So this way of writing should be meaningless .
But the compiler will check the decoration , Where relevant , Must be of the same type .
Other uses ? To be added .
边栏推荐
- uni-app进阶之创建组件/原生渲染【day9】
- DSP online upgrade (3) -- how to burn two projects in the on-chip flash of a DSP chip
- Definition of annotations and annotation compiler
- POI implements operation to generate word tables and operate chart data in word
- DSP online upgrade (4) -- functions implemented by bootloader
- Concept of naive Bayes
- How to be an interesting person
- Matplotlib 绘制圆环图的两种方法!
- Esp8266/esp32 +1.3 "or 0.96" IIC OLED pointer clock
- 东方甄选双语直播火爆出圈,新东方转型初见端倪
猜你喜欢

简易的安卓天气app(三)——城市管理、数据库操作

DSP online upgrade (1) -- understand the startup process of DSP chip

AI越进化越跟人类大脑像!Meta找到了机器的“前额叶皮层”,AI学者和神经科学家都惊了...

Will the thunderstorm of Celsius be the "Lehman moment" in the field of encryption?

New year's Eve, are you still changing the bug?

Haplotype analysis using shapeit

Underlying principle of Concurrency: thread, resource sharing, volatile keyword

DSP gossip: how to save the compiled variables on the chip when the variables are defined in the code

一行代码加速 sklearn 运算上千倍

Application configuration management, basic principle analysis
随机推荐
DSP online upgrade (4) -- functions implemented by bootloader
support vector machine
基因型填充前的质控条件简介
Mythical Games宣布与韩国领先游戏发行商Kakao Games合作,推动亚太区业务扩张
程序員新人周一優化一行代碼,周三被勸退?
optional类,便利函数,创建Optional,Optional对象操作以及Optional流
[cloud based co creation] enterprise digitalization accelerates "new intelligent manufacturing"
并发编程高级部分:并行流,Tasks和Executors以及CompletableFuture类
The memory allocation of the program, the storage of local const and global const in the system memory, and the perception of pointers~
Introduction and template of segment tree Foundation (I)
cvte一面
ArCore支持的設備
Equals and hashcode
Embedded software project process and project startup instructions (example)
记一次协程环境下类成员变量污染的问题
Regression analysis - basic content
Positive results of enx-101 clinical 1b study published by English topics
WCF restful+jwt authentication
Revenue of Fortinet will increase by 29% in 2021 founder Xieqing talks about the future of network security industry
Les nouveaux programmeurs optimisent une ligne de code lundi et sont exhortés à se retirer mercredi?