3种初始化类型[重复](3 types of Initializations [duplicate])

编程入门 行业动态 更新时间:2024-10-28 04:18:55
3种初始化类型[重复](3 types of Initializations [duplicate])

可能重复: 在C ++中,以下短语意味着什么:零,初始值和初始值?

今天我开始了解C ++中3种类型的初始化:

零初始化 默认初始化 值初始化

我用Google搜索了一下,但没有找到满意的结果。 我所得到的只是一些标准。 到目前为止,我所理解的是:在值初始化的情况下,在某些情况下,数据成员可以获得等于零的值。

请用实例详细说明它们(标准)。 另请不要只提供标准文本。

谢谢

Possible Duplicate: What do the following phrases mean in C++: zero-, default- and value-initialization?

Today I came to know about 3 types of initialization in C++:

Zero Initialization Default Initialization Value Initialization

I have googled about it but I found no satisfactory results. All I get is a few standards. What I have understood until now is this: in case of value initialization, a data member can get value equal to zero in some cases.

Please elaborate them (standards) with examples. Also please don't just provide the text from the standard.

Thanks

最满意答案

初始化的类型是指语言语法。 这里有两个例子:

T * p1 = new T; T * p2 = new T();

对象*p1是默认初始化的,对象*p2是值初始化的。

初始化的效果取决于类型T :1)如果T是一个基本类型,那么默认初始化什么也不做(即对象保持未初始化 ),而初始化值等于零初始化,并且意味着对象被设置为零。

2)如果T是一个集合(即没有构造函数或析构函数或赋值运算符的类),则每个元素都是递归的默认或值初始化。

3)如果T是类类型的,并且具有用户定义的构造函数,那么默认和初始值都会引发对默认构造函数的调用。

请注意,具有构造函数的类的成员对象可以进行默认或初始化:

struct Foo { int x; int y; Foo() : x() { } };

现在当你说Foo a; 那么a就是默认初始化的,所以调用默认的构造函数。 这反过来导致ax值 - 即零初始化,而ay保持默认 - 即未初始化。

(请注意,虽然在C ++ 11中对值进行初始化并不是真的可能,但可以使用大括号初始化来引起值初始化,就像在Foo a{};一样(这与Foo a;行为完全相同Foo a;在我们的例子中,由于第三段)。))

The types of initialization refer to the language grammar. Here are two examples:

T * p1 = new T; T * p2 = new T();

The object *p1 is default-initialized, and the object *p2 is value-initialized.

The effect of the initialization depends on the type T: 1) If T is a fundamental, then default-initialization does nothing (i.e. the object is left uninitialized), while value initialization equals zero initialization in that case and means the object is set to zero.

2) If T is an aggregate (i.e. class without constructors or destructor or assignment operator), then each element is recursively default- or value-initialized.

3) If T is of class-type and does have user-defined constructors, then both default- and value-initialization cause a call to the default constructor.

Note that member objects of classes with constructors can in turn be default- or value-initialized:

struct Foo { int x; int y; Foo() : x() { } };

Now when you say Foo a; then a is default-initialized, so the default constructor is called. This in turn causes a.x to be value-, i.e. zero-initialized, while a.y remains default-, i.e. un-initialized.

(Note that it's not really possible to value-initialize an automatic object, though in C++11, brace-initialization may be used to cause value-initialization, as in Foo a{};. (This behaves exactly the same as Foo a; in our example, consequent to the third paragraph.))

更多推荐

本文发布于:2023-07-09 14:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1087122.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:初始化   类型   types   Initializations   duplicate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!