当前位置:网站首页>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 .
边栏推荐
- Advanced CAD exercises (I)
- 最终一致性性分布式事务 TCC
- kubernetes之ConfigMap
- Re9:读论文 DEAL Inductive Link Prediction for Nodes Having Only Attribute Information
- 物联网工业级串口转WiFi转网口转以太网模块的选型
- Re7:读论文 FLA/MLAC Learning to Predict Charges for Criminal Cases with Legal Basis
- Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
- Class initialization mechanism of JVM
- 综合设计一个OPPE主页--顶部,头部的设计
- Test cases should never be used casually, recording the thinking caused by the exception of a test case
猜你喜欢

Finally, someone explained the red blue confrontation clearly

DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库

RE9: read the paper deal inductive link prediction for nodes having only attribute information

研发效能的道与术 - 道篇

The "nuclear bomb level" log4j vulnerability is still widespread and has a continuing impact

TDengine 落地协鑫能科,数百亿数据压缩至 600GB

面试时候常说的复杂度到底是什么?

Comprehensively design an oppe homepage -- Design of star models

技术风向标 | 云原生技术架构成熟度模型解读

可信隐私计算框架“隐语”开源专家观点集锦
随机推荐
Want the clouds in the picture to float? Video editing services can be achieved in three steps with one click
可信隐私计算框架“隐语”开源专家观点集锦
Pat grade a 1046 shortest distance
VS2017打开项目提示需要迁移的解决方法
该怎么写单元测试呢
《硅谷之谜》读后感
请问一下各位大佬,mysql-cdc建表如何指定表的字符集呢,在官网没找到相应的连接器参数。我读一个
PAT甲级 1044 Shopping in Mars
PAT甲级 1049 Counting Ones
MVC和ECS两种设计架构的初浅理解
PAT甲级 1046 Shortest Distance
Pat grade a 1050 string subtraction
C# 给Word每一页设置不同文字水印
终于有人把红蓝对抗讲明白了
Reflections on the mystery of Silicon Valley
【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)
Sword finger offer special assault edition day 11
Final consistency distributed transaction TCC
Clojure Web Development -- ring user guide
Acl-ijcai-sigir top conference paper report meeting (AIS 2022) Note 3: dialogue and generation