当前位置:网站首页>Protection of shared data
Protection of shared data
2022-04-23 06:37:00 【*Flowers bloom on the street】
C++ There are many measures to protect the security of data , Such as private Data members of protection classes, etc . But for some common data , Such as function real participation formal parameters, etc , We can access the same data object in different ways on different occasions . Sometimes inadvertent misoperation will change the value of data .
One 、 Regular object
It is necessary to enable data to be shared among functions , And ensure that it is not arbitrarily modified , have access to const limit , That is, data is defined as read-only .
Use... When defining objects const limit , Call it Regular object .
The general form of the definition is :
Class name const Object name ( Argument list );
or const Class name Object name ( Argument list );
Such as : const student stu1;
student const stu2 (20060102,“ Li Si ”,90) ;
1) The data members in the constant object are const Of , There has to be an initial value . In any case , Data members in constant objects cannot be modified .
2) In addition to the default constructor and default destructor , Constant objects can only call constant member functions .
Such as :stu1.num=20060103;//error
stu1.display( );//error
3) In actual programming , Sometimes Be sure to modify the value of a data member in the constant object. You can declare the data member as mutable( Variable ) To change its value .
The declaration form is :
mutable Data member type List of data member names ;
Such as : mutable int num;// Variable data members
Two 、 Constant data member
Use... When defining members const limit , Call it Constant data member .
The general form of the definition is :
const Type name Member name ;
Such as : const int num;
explain : Values of constant data members , Can only be initialized through the constructor's initialization table .
3、 ... and 、 Constant member function
Use... When defining member functions const limit , Call it Constant member function .
The general form of the definition is :
Return type Function name ( List of formal parameters ) const
{ The body of the function }
explain :
1) Constant member functions can only reference data members in this class , You can't change its value .
2) Whether you declare or define a constant member function, you should add const keyword . Constant member functions can access const Data member , You can also visit non const Data members of .const Data members can be const Member function access , It can also be used by non const Member function access .
Data member |
Non member function |
Constant member function |
Extraordinary data member |
allow access to , You can modify |
allow access to , Do not modify |
Constant data member |
allow access to , Do not modify |
allow access to , Do not modify |
Constant object data members |
Access and modification are not allowed |
allow access to , Do not modify |
Description of constant member function :
1) In a class , If the values of some data members are allowed to be modified , The values of other data members cannot be modified , Some data members can be declared as const( Constant data member ), So that its value cannot be modified . Ordinary member functions can modify ordinary data members , But you can only access the values of constant data members .
2) If it is required that the values of all data members are not allowed to change , You can declare an object as const Of ( Regular object ), You can only use const Member functions access data . And its value cannot be modified
3) If you define a constant object , Only one of them can be called const Member functions , Instead of calling non const Member functions . If you need to access data members in constant objects , All member functions in a constant object can be declared as const Member functions , However, you should ensure that the data members in the object are not modified in the function .
4) Member functions in constant objects are not necessarily constant member functions . If the member function in the constant object does not add const Statement ,C++ Treat it as a non member function .
5) A constant member function cannot call another non constant member function .
Four 、 A constant pointer to an object
Constant pointer yes const Of You can't change the direction , but The object pointed to by the constant pointer is not necessarily const Of .
A constant pointer to an object defines the form :
Class name *const Pointer variable name ;
Such as : student stu;
student *const p= &stu ;
Usually , Use Constant pointer as formal parameter of function , The purpose is not to allow the value of pointer variables to be changed during function execution , Make it always point to the original object . If you try to modify the value of a constant pointer parameter during function execution , There will be compilation errors .
Pointer variable definition form pointing to constant object :
const Class name * Pointer variable name ;
Such as : const student*p;// The object pointed to by the pointer variable is const
explain : A pointer to a constant object , It cannot change the value of the object pointed to by it , But the value of the pointer variable itself can be changed , Therefore, you can define without initializing .
The difference between a constant pointer to an object and a pointer to a constant object :
student *const p =&d;
const student *p;
1) If an object has been declared as a constant object , You can only point to a constant object with a pointer variable .
2) If you define a pointer variable that points to a constant object , Even if it points to a non const The object of , The object it points to cannot be changed by pointer .
3) A pointer to a constant object is used as a function parameter , The purpose is to protect the object pointed to by the formal parameter pointer , Make it unmodified during function execution . Use constant pointers as formal parameters of functions , The purpose is not to allow the value of pointer variables to be changed during function execution , Make it always point to the original object . If you try to modify the value of a constant pointer parameter during function execution , There will be compilation errors .
5、 ... and 、 Common references to objects
stay C++ In the program , Constant pointers and references to objects are often used as function parameters . This can ensure data security , So that the data cannot be modified arbitrarily in the function , When calling a function, you don't have to pass a copy of the argument object , Greatly reduce the space and time overhead of function calls .
Objects often refer to the definition form :
const Class name & Reference variable name ;
版权声明
本文为[*Flowers bloom on the street]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230547207135.html
边栏推荐
- 1006 finding a mex (hdu6756)
- 【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
- 【UDS统一诊断服务】四、诊断典型服务(2)— 数据传输功能单元
- MySQL groups are sorted by a field, and the first value is taken
- Export of data
- 搭建openstack平台
- 【UDS统一诊断服务】一、诊断概述(4)— 基本概念和术语
- 【无标题】
- Record the installation and configuration of gestermer on TX2, and then use GST RTSP server
- Rust 中的指针:Box、Rc、Cell、RefCell
猜你喜欢
【UDS统一诊断服务】三、应用层协议(1)
【UDS统一诊断服务】(补充)五、ECU bootloader开发要点详解 (1)
【UDS统一诊断服务】四、诊断典型服务(3)— 读故障信息功能单元(存储数据传输功能单元)
利用文件保存数据(c语言)
ArcGIS表转EXCEL超出上限转换失败
非参数化相机畸变模型简介
从源代码到可执行文件的过程
1007 go running (hdu6808) in the fourth game of 2020 Hangzhou Electric Multi school competition
P1586 solution to tetragonal theorem
How SYSTEMd uses / etc / init D script
随机推荐
带默认模板实参的类模板与模板模板形参的匹配
Graduation project, viewing screenshots of epidemic psychological counseling system
Option的正确打开方式
gcc ,g++,gdb的安装
Common shortcut keys of IDE
Feign请求日志统一打印
Easy to use data set and open source network comparison website
爬取小米有品app商品数据
Rust的闭包类型(Fn, FnMut, FnOne的区别)
Dynamic creation and release, assignment and replication of objects
破解滑动验证码
A solution to replace not in in SQL
Import of data
ArcGIS表转EXCEL超出上限转换失败
PM2 deploy nuxt related commands
用C语言实现重写strcmp等四个函数
相机标定:关键点法 vs 直接法
安全授信
【UDS统一诊断服务】三、应用层协议(1)
Record the installation and configuration of gestermer on TX2, and then use GST RTSP server