原始类型还有C ++中的构造函数? [英] Do primitive types have also constructors in C++?

查看:161
本文介绍了原始类型还有C ++中的构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了博士Bjarne Stroustrup书C ++编程语言第三版内置类型在C ++中的构造函数在10.4.2节。

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.

但是下面的链接说明POD类型不能有构造函数:

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

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

这是真的吗?原型类型在C ++中也有构造函数?

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

推荐答案

Bjarne的意思是你可以写 int 56)或甚至 int()构造一个整数。链接的含义是,如果没有声明构造函数, struct / class 只是一个POD。所以Bjarne谈论原始的非结构类型和链接谈论结构/类,所以这两个源可以共存而不矛盾对方。

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:


POD类型的非静态数据成员必须是公开的,并且可以是任何这些类型

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

当然,这只能用于结构体。 int 没有数据成员。所以虽然链接从来没有直接提到它,它只指向结构和类。

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.

这篇关于原始类型还有C ++中的构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆