当前位置:网站首页>Differences between the use of structs and classes
Differences between the use of structs and classes
2022-07-26 16:26:00 【A bowl of glutinous rice balls】
Some time ago, I encountered a problem when writing thrust superposition , At first, I used two lists to store the speed obtained by thrust and direction respectively 、 Duration of speed , Subscripts correspond to each other . Later, I felt that I could use structures to store speed and duration , In this way, you only need a list management , You can use less than one traversal , At the same time, better object-oriented . Then when I use the structure to change, I find that the fields in the structure cannot be directly used for addition, subtraction and other operations , Because the fields in the structure are placed on the stack , Value type . Later, the structure was changed into a class to solve this problem , Because classes are stored in the heap , Is the reference type . So I want to summarize the difference between the two :
1. Structure field cannot be initialized , The fields of class can be initialized

2. A structure is a value type , Store on stack ; Class is a reference type , Store in the pile

3. Structs can no longer write parameterless constructors , Classes can be ; However, you can write constructors with parameters


4. All fields must be assigned values in the constructor of the structure , Classes do not have to be assigned all

5. Structures create objects in a somewhat different way

6. To sum up , When to use ?
(1) Structures are stored on the stack , and Stack has 1 Characteristics , It's just that the space is small , But the access speed is faster , Large heap space , But the access speed is relatively slow . So when we describe 1 When a lightweight object , It can be defined as a structure to improve efficiency . For example , rectangular , Color , These objects are lightweight objects , Because describing them , Only a few fields are needed . When describing 1 When you're a heavyweight object , We know that class objects are stored in heap space , We define the heavyweight object as a class . Although they can contain data structures of data members and function members , But the difference between structs and classes is , The structure is a value type and does not require heap allocation , The variables of the structure directly contain the data of the structure , The variables of the class contain references to data .
(2) When we pass values to variables , I just want to pass a copy of the object , Not the reference address of the object , Then you can also use the structure at this time .
(3) I see some places where structures are used in projects , Generally, it is the place where the field is pure quantity or pure string , So an object is a pure data type , No complicated method 、 Logic 、 Structure can be considered for operation . This is the main reason for using structures at present , There is also a structure in the editor ( such as Unity) It will be more convenient to display on .
边栏推荐
- 阿里云DMS MySQL云数据库建表报错,求解!!
- The process and harvest of developing browser plug-ins with clojurescript
- Clojure Web 开发-- Ring 使用指南
- Reflections on the mystery of Silicon Valley
- SQL statement -- single line comment and multi line comment
- 2022 Niuke summer multi school training camp 1 (acdgij)
- 【E-MR】NameNode的错误恢复记录
- 6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
- Octree establishes map and realizes path planning and navigation
- [physical simulation] ultra simple shape matching simulates rigid body motion
猜你喜欢

PAT甲级 1050 String Subtraction
![[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)](/img/84/640de0bf779cd45498204909be56d1.png)
[ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
![[BJDCTF2020]Easy MD5](/img/6a/61a4b5624c33f1f334bea344cfa2c8.png)
[BJDCTF2020]Easy MD5

技术风向标 | 云原生技术架构成熟度模型解读
![[physical simulation] the principle and practice of the simplest shape matching](/img/1e/d91ed992bc648d90d0c68bfe541d7e.jpg)
[physical simulation] the principle and practice of the simplest shape matching

PAT甲级 1045 Favorite Color Stripe
Specific practice cases of "card note taking method" in Siyuan

FTP protocol

【ARM学习(9) arm 编译器了解学习(armcc/armclang)】

综合设计一个OPPE主页--布局与初始化
随机推荐
6种方法帮你搞定SimpleDateFormat类不是线程安全的问题
面试时候常说的复杂度到底是什么?
Finally, someone explained the red blue confrontation clearly
物联网工业级串口转WiFi转网口转以太网模块的选型
JS API summary of Array Operations
vlang捣鼓之路
Final consistency distributed transaction TCC
Pat grade a 1049 counting ones
[BJDCTF2020]Easy MD5
CAD进阶练习题(一)
TKE集群节点max-pod是如何配置的
Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
初识OpenGL (3)片段着色器(Fragment Shader)
【Flutter -- 进阶】打包
spark-streaming状态流之mapWithState
Test cases should never be used casually, recording the thinking caused by the exception of a test case
PAT甲级 1046 Shortest Distance
ZABBIX 6.2.0 deployment
Class initialization mechanism of JVM
The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact