My understanding of classes and structures

Source: Internet
Author: User

1. First of all, I have learned a little bit about it.

Class A is a reference type. Functions can be automatically constructed by default.

The B structure is a value type, and all value types are based on the structure type. The default constructor cannot be used automatically.

When comparing C classes and structures, they both compare their references. But why do we think they are the same or different value types, this is because tcompare is an interface first for value types. In this way, even if the two value types have different address spaces, they will not be considered to be the same, but the same. They should be implemented by the two interfaces. What is the other interface, have time to study.

A simple syntax is supported for the initial values of fields. Classes are supported, but structures are not supported.

Struct structstu {int I; // int I = 5. Why is this writing incorrect? struct cannot customize the default constructor. Int J; Public structstu (int ii) {I = II; // The constructor must Initialize all variables. J = II; // if this line of code does not exist, it is also incorrect .} }

D. When should I use the structure?

Similar to a simple value type. All simple value types are based on the structure type.

The size of the data member is 16 kb. Because the memory of the structure is allocated on the stack, if frequently used, data will be copied in large quantities.

No special initialization operation is required. This is because the structure does not provide Default constructors.

The F structure is a lightweight data encapsulation structure.

My doubts:

What is the difference between allocating structures to stacks and allocating them to stacks?

Stack is allocated by the system, and the original stack is advanced. The address is continuous and efficient.

The heap is allocated by programmers. The address is not consecutive and the efficiency is slow.

To sum up, the structure is a special type. If you do not understand it clearly, it will be used less. Otherwise, it will cause a great waste of program resources and low computing efficiency.

I have time to write a schema topic!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.