简体   繁体   English

关于 c++ 中的结构初始化

[英]Regarding struct initialization in c++

I have Struct like these我有这样的结构

 typedef struct sample
 {
       double x,
       double y,
       double z
 }s1;
 s1 s;

will the content in s variable initialized or not? s 变量中的内容是否会初始化?

What will be the values of x,y,z? x,y,z 的值是多少?

thanks谢谢

x , y and z won't be initialized if s is defined in a function scope.如果s在 function scope 中定义, xyz将不会被初始化。 They would be containing some unspecified values.它们将包含一些未指定的值。 At file scope the data members would be initialized to their default values.在文件 scope 中,数据成员将被初始化为其默认值。

In C++ however you can have a constructor initializer list to initialize the data members在 C++ 但是你可以有一个构造函数初始化列表来初始化数据成员

For example例如

struct ABC
{
   int x;
   int y;

   ABC(): x(1),y(2){}
};

ABC s; // x and y initialized to 1 and 2 respectively

In C++ you also have default initialization and value initialization to initialize data members.在 C++ 中,您还可以使用默认初始化和值初始化来初始化数据成员。

In the code you presented, the fields will be uninitialized.在您提供的代码中,这些字段将未初始化。 You can add a constructor (if you need/can), or in case you need the POD-ness (some part of your code depends on some of those properties) and you cannot add a constructor, you can still value-initialize the struct (ie set each member to 0) at the place of use:您可以添加一个构造函数(如果需要/可以),或者如果您需要 POD 特性(您的代码的某些部分取决于其中一些属性)并且您不能添加构造函数,您仍然可以对结构进行值初始化(即设置每个成员为0)在使用的地方:

struct sample        // typedef not required
{
   double x,
   double y,
   double z
};
sample s = sample(); // will set all members to 0.0

Now, if you want to initialize different members with some particular values, because it is an aggregate you can use aggregate initialization:现在,如果你想用一些特定的值初始化不同的成员,因为它是一个聚合,你可以使用聚合初始化:

sample s = { 1.0, 3.0 };

That will set x to 1.0 , y to 3.0 .这会将x设置为1.0 ,将y设置为3.0 Since there is no value for z , the compiler will set it to 0.0 .由于z没有值,编译器会将其设置为0.0 Note that this means that sample s = {};请注意,这意味着sample s = {}; is equivalent to sample s = sample();相当于sample s = sample();

If it is C++, you could make constructor.如果是 C++,可以做构造函数。

struct s1
{
  s1( const double x = 0.0, const double y = 0.0, const double z = 0.0 )
  : x(x), y(y), z(z)
  {
  };
  double x;
  double y;
  double z;
};

s1 s;

Built-in types like double and int are initialised if the variable is static or at namespace /file scope, otherwise - for efficiency reasons - they're not initialised unless a constructor indicates that's useful.如果变量是static或在namespace / 文件 scope 中,则会初始化诸如doubleint之类的内置类型,否则 - 出于效率原因 - 它们不会被初始化,除非构造函数表明这是有用的。

Note: this answer addresses the "s1 s;"注意:此答案针对“s1 s;” situation you describe.你描述的情况。 It is possible to provide an explicit initialisation when defining the variable, but that's another case.定义变量时可以提供显式初始化,但这是另一种情况。

To add a constructor so:要添加构造函数:

struct X
{
    X() : x_(0), y_(0), z_(0) { }
    double x, y, z;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM