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!