Differences between C ++ struct and class

Source: Internet
Author: User

From: http://blog.csdn.net/fengxinziyang/article/details/5909237 (PS:In my book visual c ++ from beginners to masters, the struct can also be inherited or inherited, or there can be virtual functions)

Differences:

Structure is a type of custom data declared with the keyword struct. Similar to a class, it can also contain constructors, constants, fields, methods, attributes, indexers, operators, and nested types. However, the structure is a value type.

1. the constructor of the structure is different from the constructor of the class.

A. the structure cannot contain explicit non-parameter constructors. Structure members are automatically initialized to their default values.

B. The structure cannot contain the following initialization classes: Base (argument-list );

2. For instance field members in the structure, the value assignment cannot be initialized during the declaration.

3. After declaring the structure type, you can use the new operator to create a constructor object or use the new keyword. If new is not used, the field remains unassigned and the object is unavailable before all fields are initialized.

4. The structure does not support inheritance, that is, a structure cannot inherit from another structure or class, and cannot be used as the base class of a class. However, the structure inherits from the base class object. Structure can also implement interfaces.

5. When should I use the structure? The structure is simple and useful, but remember that the structure created in the stack is a value type, and the class is a reference type. Whenever a frequently used type is required, and in most cases this type is only some data, the structure can achieve better performance than the class.

Structure is a value type, which affects performance. However, according to the structure, this impact may be positive or negative. The positive effect is that when memory is allocated to the structure, the speed is very fast because they are inline or saved in the stack. When the structure is deleted beyond the scope, the speed is also very fast. On the other hand, as long as the structure is passed as a parameter or a structure is assigned to another structure (for example, a = B, where A and B are structures), all the content of the structure will be copied, for classes, only references are copied. In this way, there will be performance loss, depending on the size of the structure, the performance loss is also different. Note that the structure is mainly used for small data structures. But when the structure is passed to the method as a parameter, it should be passed as the ref parameter to avoid performance loss-at this time, only the address of the structure in the memory is passed, in this way, the transfer speed is as fast as the transfer speed in the class. In this case, you must note that the called method can change the value of the structure.

another point: struct and class should be essentially the same, but the default access permissions are different (struct is public by default, class is private by default ). to retain struct, most of the reasons are for compatibility with C (in a Article of C ++, I think there is still some truth ). the class should be an extension of struct. In addition to setting the access type of Members, it can also have its own member functions, which can inherit and derive, as if struct can do all the class operations, but in turn, it will not work. If there is no requirement to protect data, I will use struct to do what struct can do, which is relatively simple.

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.