Do primitive types also have constructors in C ++?

I read in the book of Dr. Bjarn Straustup "C ++ Programming Language" the 3rd edition, built in types, also contains constructors in C ++ in section 10.4.2.

But then the following link says that POD types cannot have constructors:

http://www.parashift.com/c++-faq-lite/pod-types.html

What is right? Do primitive types also have constructors in C ++?

+6
source share
1 answer

What Bjarne means is that you can write int(56) or even int() to construct an integer. What the link means is that struct / class is only a POD if it does not have a declared constructor. Therefore, Bjarn talks about primitive non-structural types, and the link talks about structures / classes, so the two sources can coexist without contradiction with each other.

Here is part of the definition from the link:

POD type non-static data elements must be publicly available and can be any of these types

Of course, this can only be used for structures. int has no "data members". Therefore, although a link never mentions it directly, it refers only to structures and classes.

+8
source

Source: https://habr.com/ru/post/972589/


All Articles