当前位置:网站首页>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 .
边栏推荐
- Is it safe for Guoyuan futures to open an account online? What is the account opening process?
- Clojure 运行原理之编译器剖析
- 面试时候常说的复杂度到底是什么?
- [RCTF2015]EasySQL
- kubernetes之探针
- Comprehensively design an oppe homepage -- Design of star models
- 我的sql没问题为什么还是这么慢|MySQL加锁规则
- Clojure Web 开发-- Ring 使用指南
- [physical simulation] ultra simple shape matching simulates rigid body motion
- What is the complexity often said during the interview?
猜你喜欢

Mapwithstate of spark streaming state flow
Final consistency distributed transaction TCC

测试用例千万不能随便,记录由一个测试用例异常引起的思考

微信小程序---网络数据请求
Specific practice cases of "card note taking method" in Siyuan

Bugku login1

Simulation of three-phase voltage source inverter based on SISOTOOL pole assignment PI parameters and pless

Re7: reading papers fla/mlac learning to predict charges for critical cases with legal basis

可信隐私计算框架“隐语”开源专家观点集锦

该怎么写单元测试呢
随机推荐
Clojure 运行原理之字节码生成篇
Is it safe for Guoyuan futures to open an account online? What is the account opening process?
2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
Collection of open source expert opinions on trusted privacy computing framework "argot"
请问一下各位大佬,mysql-cdc建表如何指定表的字符集呢,在官网没找到相应的连接器参数。我读一个
DTS搭载全新自研内核,突破两地三中心架构的关键技术|腾讯云数据库
C # set different text watermarks for each page of word
kubernetes之ConfigMap
C#事件和委托的区别
How to configure tke cluster node Max pod
Pat grade a 1045 favorite color stripe
Class initialization mechanism of JVM
2022 latest Beijing Construction Safety Officer simulation question bank and answers
Advanced CAD exercises (I)
国元期货网上开户安全吗?开户办理流程是怎样的?
PAT甲级 1044 Shopping in Mars
Pat grade a 1046 shortest distance
Final consistency distributed transaction TCC
基于sisotool极点配置PI参数及基于Plecs的三相电压源逆变器仿真
Question collection come and ask nllb authors! (Zhiyuan live issue 24)