Do primitive types have also constructors in C++?

六眼飞鱼酱① 提交于 2019-12-06 02:16:34

问题


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++?


回答1:


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.



来源:https://stackoverflow.com/questions/24868072/do-primitive-types-have-also-constructors-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!