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

Do Built-in Types in C Actually Have Default Constructors?

DDD
Release: 2024-12-26 21:27:10
Original
443 people have browsed it

Do Built-in Types in C   Actually Have Default Constructors?

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template