[c++] Comparison of structure struct and class class

[c++] Comparison of structure struct and class class

Reference: The difference between structure and class https://blog.csdn.net/u013341034/article/details/50884967


1. Structures and classes are very similar:


1. The definition and usage are very similar, examples are as follows:
public struct Student
{
    string Name;
    int Age;
}
public class Question
{
    int Number;
     string Content;

}


Use :
Student s=new Student();
Question q=new Question();
2, both are container types, which means they can contain other data types as members.

3. Both have members, including: constructors, methods, properties, fields, constants, enumeration types, events, and event handlers.

4. The members of both have their own access scope. For example, you can declare one member as Public and another member as Private.

5. Both can implement the interface.

6. Both can expose a default property, but only if the property takes at least one argument.

7. Both can declare and trigger events, and both can declare delegates.

Second, the main difference between structures and classes

1. A structure is a value type, while a class is a reference type. Value types are used to store the value of the data and reference types are used to store references to the actual data.

      Then the structure is used as a value, and the class operates on the actual data by reference.

2. Structures use stack storage (Stack Allocation), while classes use heap storage (Heap Allocation).

3. All structure members are Public by default, and the variables and constants of the class are Private by default, but other class members are Public by default.

4. Structure members cannot be declared Protected, while class members can.

5. The structure variable declaration cannot specify the initial value, use the New keyword to initialize the array, but the class variable declaration can.

6. Structures cannot declare a default constructor, that is, a non-shared constructor that does not have parameters, but classes do not have this restriction.

7. Both can have shared constructors. The shared constructor of a structure cannot take parameters, but the shared constructor of a class can take or not take parameters.

8. Structures are not allowed to declare destructors, but classes do not have this restriction.

9. The instance declaration of the structure does not allow the initialization of the contained variables, and the class can initialize the variables at the same time when declaring the instance of the class.

10. The structure is implicitly inherited from the ValueType class, and cannot inherit from any other type, and the class can continue from any class other than ValueType.

11. Structures cannot be inherited, but classes can.

12. Structures never terminate, so the CLR does not call the Finalize method on any structures. The class is terminated by the memory recycling process. When the memory recycling process detects a class that has no effect, it will call the Finalize method of the class.

13. The definition of structure is: some data are not only interrelated, but also describe a complete thing together, such as: the overall information of a student, student number, name, gender, etc. Classes are used for data that needs to be more hierarchical.

14. A class is an abstraction that reflects real things, and the function of a structure is just a packaging that contains different types of data. The structure does not have the inheritance polymorphism of a class. 

15,构造函数是为了初始化类的字段而存在的,而结构体并不需要初始化就能使用,因此,结构体中并不存在默认的构造函数。
结构:
没有默认的构造函数,但是可以添加构造函数
    没有析构函数
    没有 abstract 和 sealed(因为不能继承)
    不能有protected 修饰符
    可以不使用new 初始化
在结构中初始化实例字段是错误的
类: 
 有默认的构造函数
 有析构函数
 可以使用 abstract 和 sealed 
有protected 修饰符
必须使用new 初始化

三.如何选择结构还是类
   
1.  堆栈的空间有限,对于大量的逻辑的对象,创建类要比创建结构好一些
2.   结构表示如点、矩形和颜色这样的轻量对象,例如,如果声明一个含有 1000 个点对象的数组,则将为引用每个对象分配附加的内存。在此情况下,结构的成本较低。
3.  在表现抽象和多级别的对象层次时,类是最好的选择
4.  大多数情况下该类型只是一些数据时,结构时最佳的选择 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817906&siteId=291194637