typedef struct:默认初始化 [英] typedef struct : Default Initialization

查看:367
本文介绍了typedef struct:默认初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  typedef struct foo 
{
bool my_bool;
int my_int;
} foo;

在上面的示例中,我理解 my_bool 会随机初始化true或false,但是 my_int 如何?我假设 my_int 将默认初始化为0,但似乎不是这样。



以这种方式定义结构似乎不兼容初始化列表,那么最好的方法是将 my_bool my_int 初始化为false和0。

解决方案

类型不会初始化。只有某些类型的对象得到初始化。它们如何以及何时被初始化取决于相应对象的定义方式和位置。你没有在你的问题中提供任何对象的定义,所以你的问题本身并没有什么意义 - 它缺乏必要的上下文。



例如,如果你定义类型为的静态对象foo

  static foo foo_object; // zeros 

它将被自动零初始化,因为所有静态持续时间的对象总是自动零 -



如果定义类型为 foo 的自动对象而不使用初始化器,它将保持未初始化

  void func()
{
foo foo_object; // garbage
}

如果定义类型为 foo 与聚合初始化器,它将根据初始化器初始化

  void func )
{
foo foo_object1 = {1,2}; // initialized
foo foo_object2 = {}; //用零初始化
}

如果你使用并且不提供初始化器,它将保持未初始化

  foo * p = new foo;但是如果你使用(),那么你可以使用 



<
初始化,它将被初始化。

  foo * p = new foo // zeros in`* p` 

如果创建类型 使用 foo()表达式,该表达式的结果将初始化为零。

  bool b = foo()。my_bool; // zero 
int i = foo()。my_int; // zero

所以,在你的特定情况下,初始化细节依赖于现在你创建对象的类型,而不是你的类型本身。您的类型本身没有固有的初始化工具,并且不会以任何方式干扰初始化。


typedef struct foo
{
    bool my_bool;
    int my_int;
} foo;

In the example above I understand that my_bool will be initialized randomly to either true or false but what about my_int? I assumed that my_int would be default initialized to 0 but that seems not to be the case.

Defining structs in this way appears to be incompatible with initialization lists so what is the best way to initialize my_bool and my_int to false and 0 respectively?

解决方案

Types don't get "initialized". Only objects of some type get initialized. How and when they get initialized depends on how and where the corresponding object is defined. You provided no definition of any object in your question, so your question by itself doesn't really make much sense - it lacks necessary context.

For example, if you define a static object of type foo

static foo foo_object; // zeros

it will be automatically zero-initialized because all objects with static duration are always automatically zero-initialized.

If you define an automatic object of type foo without an initializer, it will remain uninitialized

void func()
{
   foo foo_object; // garbage
}

If you define an automatic object of type foo with an aggregate initializer, it will be initialized in accordance with that initializer

void func()
{
   foo foo_object1 = { 1, 2 }; // initialized
   foo foo_object2 = {}; // initialized with zeros
}

If you allocate your object with new and provide no initializer, it will remain uninitialized

foo *p = new foo; // garbage in `*p`

But if you use the () initializer, it will be zero-initialzed

foo *p = new foo(); // zeros in `*p`

If you create a temporary object of type foo using the foo() expression, the result of that expression will be zero-initialized

bool b = foo().my_bool; // zero
int i = foo().my_int; // zero

So, once again, in your specific case the initialization details depend on now you create the object of your type, not on your type itself. Your type itself has no inherent initialization facilities and doesn't interfere with the initialization in any way.

这篇关于typedef struct:默认初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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