Do Built-In Types Have Default Constructors?
The question arose after reading an article suggesting int() yields 0 due to a default constructor, leading to the belief that primitive types inherently lack constructors. However, Section 10.4.2 of "TC PL" implies otherwise, claiming built-in types possess default constructors.
Bjarne Stroustrup's Response
When queried about the apparent discrepancy, Bjarne Stroustrup clarified that built-in types do not have actual constructor calls. Instead, they behave as if they do, and the constructor-like notation is interpreted by the compiler.
Technical Answer
Strictly speaking, no. Built-in types do not possess default constructors.
Functional Considerations
Despite the lack of actual constructors, the syntax for initializing built-in types closely resembles constructor invocation. The use of braces ({}) when initializing enables the zero initialization of integers (int{0}) and copies from parentheses-enclosed expressions (int a{b}).
Conclusion
While built-in types do not have default constructors, their initialization syntax and behavior emulate their presence. They seemingly possess default and copy constructors, allowing for initialization and value assignment. This pseudo-constructor mechanism simplifies initialization and promotes code readability. However, it's crucial to recognize that these types lack true constructors.
The above is the detailed content of Do Built-in Types in C Actually Have Default Constructors?. For more information, please follow other related articles on the PHP Chinese website!