Structure (struct) and Class (class)

Source: Internet
Author: User

Similar to a class, the structure indicates the data structure that can contain data members and function members.

Different from the class,The structure is value type and does not require heap allocation.

Structure Type VariablesDirectly include structured data,Variables of the class type include references to data.(This variable is called an object ).

The struct type is applicable to lightweight objects such as dots, rectangles, and colors. Although a vertex may be represented as a class,

However, the structure is more effective in some schemes. In some cases, the structure cost is low.

For example, if an array containing 1000 vertex objects is declared, additional memory will be allocated to each referenced object.

The structure can declare constructors, but they must contain parameters.

The default (No parameter) constructor of the declared structure is incorrect.

The default constructor is always provided to initialize the structure members as their default values.

It is incorrect to initialize the instance field in the structure. The instance object must be initialized in the class.

When you use the new operator to create a structure object, this structure object is created and an appropriate constructor is called.

Different from the class, the new operator can be used for structure instantiation. If new is not used, the field remains unassigned and the object is unavailable before all fields are initialized.

For structures, there is no inheritance like classes. A structure cannot inherit from another structure or class, and cannot be the base of a class.

However, the structure inherits from the base class object. The structure can implement interfaces in the same way.

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.