Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do primitive types have also constructors in C++?

I have read in Dr. Bjarne Stroustrup Book "The C++ Programming Language" 3rd edition that built in types have also constructors in C++ in section 10.4.2.

But then the following link says that POD types can't have constructors:

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

Which is true? Do primitive types have also constructors in C++?

like image 751
Destructor Avatar asked Jul 21 '14 14:07

Destructor


1 Answers

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

Here is part of the definition from the link:

a POD type's non-static data members must be public and can be of any of these types

Of course, this can only hold for structs. An int has no "data members". So although the link never mentions it directly, it only refers to structs and classes.

like image 79
gexicide Avatar answered Nov 13 '22 12:11

gexicide