C++ 在结构中初始化默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16782103/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:39:01  来源:igfitidea点击:

Initializing default values in a struct

c++

提问by Joe Wilcoxson

If I needed to initialize only a few select values of a C++ struct, would this be correct:

如果我只需要初始化 C++ 结构的几个选择值,这是否正确:

struct foo {
    foo() : a(true), b(true) {}
    bool a;
    bool b;
    bool c;
 } bar;

Am I correct to assume I would end up with one structitem called barwith elements bar.a = true, bar.b = trueand an undefined bar.c?

我是正确的假设我最终会与一个struct叫项bar的元素bar.a = truebar.b = true和一个未定义的bar.c

采纳答案by taocp

Yes. bar.aand bar.bare set to true, but bar.cis undefined. However, certain compilers will set it to false.

是的。bar.abar.b设置为 true,但bar.c未定义。但是,某些编译器会将其设置为 false。

See a live example here: struct demo

在此处查看实时示例:struct demo

According to C++ standard Section 8.5.12:

根据 C++ 标准第 8.5.12 节:

if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value

如果不进行初始化,自动或动态存储时间的对象具有不确定的值

For primitive built-in data types (bool, char, wchar_t, short, int, long, float, double, long double), only globalvariables (all static storage variables) get default value of zero if they are not explicitly initialized.

对于原始内置数据类型(bool、 char 、 wchar_t 、 short 、 int 、 long 、 float 、 double 、 long double ),只有全局变量(所有静态存储变量)如果未显式初始化,则其默认值为零。

If you don't really want undefined bar.cto start with, you should also initialize it like you did for bar.aand bar.b.

如果您真的不想以 undefinedbar.c开头,您还应该像对bar.aand那样初始化它bar.b

回答by Erik van Velzen

You don't even need to define a constructor

你甚至不需要定义一个构造函数

struct foo {
    bool a = true;
    bool b = true;
    bool c;
 } bar;

To clarify: these are called brace-or-equal-initializers (because you may also use brace initialization instead of equal sign). This is not only for aggregates: you can use this in normal class definitions. This was added in C++11.

澄清一下:这些被称为大括号或等号初始化器(因为您也可以使用大括号初始化而不是等号)。这不仅适用于聚合:您可以在普通类定义中使用它。这是在 C++11 中添加的。

回答by Nicola

You can do it by using a constructor, like this:

您可以通过使用构造函数来做到这一点,如下所示:

struct Date
{
int day;
int month;
int year;

Date()
{
    day=0;
    month=0;
    year=0;
}
};

or like this:

或者像这样:

struct Date
{
int day;
int month;
int year;

Date():day(0),
       month(0),
       year(0){}
};

In your case bar.c is undefined,and its value depends on the compiler (while a and b were set to true).

在你的情况下 bar.c 是未定义的,它的值取决于编译器(而 a 和 b 被设置为 true)。

回答by Anamnian

An explicit default initialization can help:

显式默认初始化可以帮助:

struct foo {
    bool a {};
    bool b {};
    bool c {};
 } bar;

Behavior bool a {}is same as bool b = bool();and return false.

行为与 returnbool a {}相同。bool b = bool();false