Home > Backend Development > C++ > Do Built-In Types in C Have Constructors?

Do Built-In Types in C Have Constructors?

Susan Sarandon
Release: 2024-12-08 06:57:10
Original
236 people have browsed it

Do Built-In Types in C   Have Constructors?

Do Built-In Types Have Constructors?

When a temporary int is value-initialized to 0, is it because int() calls the default constructor? This is a common misconception, as built-in types do not have constructors.

Value Initialization vs. Default Constructor

Value initialization, a relatively recent concept, initializes a temporary object with a value that is appropriate for its type. For built-in types, this value is 0 for integers.

TC PL's Statement

Section 10.4.2 of TC PL states that built-in types also have default constructors. However, this statement is arguably incorrect in the context of C 98 and C 03.

Bjarne Stroustrup's Clarification

When contacted personally, Bjarne Stroustrup clarified that he considers built-in types to have constructors in a conceptual sense, despite their lack of actual constructor calls. This conceptualization is based on their constructor-like behavior, such as the initialization of values.

Conclusion

Technically, built-in types do not have constructors. However, the syntax used to initialize them (e.g., int{5}, int{}) is identical to that of constructors. Therefore, for practical purposes, it is reasonable to think of built-in types as having default constructors that initialize their values appropriately during value initialization.

The above is the detailed content of Do Built-In Types in C Have Constructors?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template