buillt

编程入门 行业动态 更新时间:2024-10-24 19:13:25
buillt-in成员的默认初始化[重复](Default initialization of buillt-in members [duplicate])

这个问题在这里已有答案:

默认构造函数,POD的初始化和C ++ 11 2中的 隐式类型转换

关于Bjarne Stroustrup的The C ++ Programming Language,4th Edition,17.6.3.1

内置成员的“默认初始化”使该成员未初始化。

引用默认编译器生成的构造函数。

但是,在17.6.2中我们有以下代码

struct S { string a; int b; }; S f(S arg) { S s0 {}; // default construction: {"",0} .. }

其中b默认初始化为0。

那么,我在这里错过了什么?

This question already has an answer here:

Default constructors, initialization of POD and implicit type conversions in C++11 2 answers

On Bjarne Stroustrup's The C++ Programming Language, 4th Edition, 17.6.3.1 it is stated that

The ‘‘default initialization’’ of a built-in member leaves that member uninitialized.

referring to the default compiler generated constructor.

However, in 17.6.2 we have the following code

struct S { string a; int b; }; S f(S arg) { S s0 {}; // default construction: {"",0} .. }

where b is default initialized to 0.

So, what am I missing here ?

最满意答案

您正在进行“ 聚合初始化 ”,而不是默认初始化。 在聚合初始化中,未指定的成员经历值初始化(例如,对于整数为零)。

You are doing "aggregate initialization," not default initialization. And within aggregate initialization, unspecified members undergo value initialization (e.g. zero for integers).

更多推荐

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

发布评论

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

>www.elefans.com

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