当前位置:网站首页>Const keyword, variable and function are decorated with const
Const keyword, variable and function are decorated with const
2022-04-23 18:15:00 【Talent、me】
const keyword
const yes constant Abbreviation , It is called constant , By const The modified entities are subject to compulsory protection , Avoid unexpected changes , It can improve the robustness and maintainability of the program .
const Variable
const int maxSize = 50; //int Type constant
const int *ptr = &maxSize; // Point to const int Pointer to type
int minSize = 100; //int type
int* const ptr1 = &minSize; // Point to int Constant pointer to type
const int* const ptr2 = &maxSize; // Point to const int Constant pointer to type
// Let's briefly describe
const int testSize = 30;
ptr = testSize; //ptr The pointer can be redirected to another variable
int num = 10;
prt1 = # // error , Constant pointers can only point to objects during initialization , Cannot change the object pointed to
It should be noted that :const The object must be initialized , Because the assignment operation cannot be performed , Can only be assigned at the time of definition .
const For functions
//const Cannot be used on output parameters , Can only be used on input parameters , Make sure that the scope of the function does not change
void copy(QString& dest, const QString sourse)
{
........
}
//const Used on the return value of a function
const int* readData()
{
}
const int* value = readData();// correct
int* value1 = readData();// error
// Because if the return value is a pointer , Must be of the same type as the return value .
//const Used at the end of a function declaration (const Member functions )
void test() const
{
//const Member functions must not modify data members or call other non members const Member functions , If... Is modified or called during this period , The compiler will report an error .
}
define ( Macro definition )
here , I have to say define This keyword
Constants are often defined as preprocessor macros .( I often use macro definitions to define constants now , Now it's slowly changing )
#define MAXSIZE (30)
int array[MAXSIZE];
The macro definition will replace the macro in the program in the preprocessing phase in the compilation phase .
Why use it const Not macro definitions ?
because const Expressions can be type checked at compile time , So I prefer const expression .
版权声明
本文为[Talent、me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210610471323.html
边栏推荐
- STM32 learning record 0008 - GPIO things 1
- 【ACM】376. 摆动序列
- 14个py小游戏源代码分享第二弹
- Crack sliding verification code
- Mysqldump backup database
- Docker 安装 Redis
- MySQL auto start settings start with systemctl start mysqld
- re正則錶達式
- Qt读写XML文件(含源码+注释)
- PowerDesigner various font settings; Preview font setting; SQL font settings
猜你喜欢
Deep learning classic network analysis and target detection (I): r-cnn
【ACM】509. 斐波那契数(dp五部曲)
[UDS unified diagnostic service] IV. typical diagnostic service (6) - input / output control unit (0x2F)
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
[UDS unified diagnostic service] v. diagnostic application example: Flash bootloader
深度学习经典网络解析目标检测篇(一):R-CNN
From introduction to mastery of MATLAB (2)
Robocode Tutorial 4 - robocode's game physics
Install pyshp Library
JD-FreeFuck 京東薅羊毛控制面板 後臺命令執行漏洞
随机推荐
From introduction to mastery of MATLAB (2)
Selenium + webdriver + chrome realize Baidu to search for pictures
Nat commun | current progress and open challenges of applied deep learning in Bioscience
Re expression régulière
Install pyshp Library
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Quantexa CDI(场景决策智能)Syneo平台介绍
re正则表达式
Nodejs installation
word frequency count
Qt读写XML文件(含源码+注释)
【ACM】455. Distribute Biscuits (1. Give priority to big biscuits to big appetite; 2. Traverse two arrays with only one for loop (use subscript index -- to traverse another array))
Reptile efficiency improvement method
[UDS unified diagnostic service] (Supplement) v. detailed explanation of ECU bootloader development points (2)
Classes and objects
解决允许在postman中写入注释请求接口方法
Excel opens large CSV format data
由tcl脚本生成板子对应的vivado工程
Auto. JS custom dialog box
Crawling mobile game website game details and comments (MQ + multithreading)