简体   繁体   English

编译器在C ++中生成的默认构造函数

[英]a compiler generated default constructor in c++

declaring a struct Table: 声明结构表:

struct Tables {
       int i;
       int vi[10];
       Table t1;
       Table vt[10];
};

Tables tt;

assuming that a user-deault contructor is defined for Table. 假设为Table定义了用户错误构造函数。

here tt.t1 will be initialized using the default contructor for Table, as well as each element in tt.vt. 在这里,tt.t1将使用Table的默认构造器以及tt.vt中的每个元素进行初始化。

On the other hand tt.i and tt.vi are not initialized because those objects are not of a class type. 另一方面,tt.i和tt.vi未初始化,因为这些对象不是类类型。

so we remain with a semi-initialized object tt. 因此,我们保留了一个半初始化对象tt。

if I understood well - if tt.i or tt.vi won't be explicitly initialized i the code, after creating tt, an error will be thrown if we try to read a value from them? 如果我理解得很好-如果不会在代码中显式初始化tt.i或tt.vi,则在创建tt之后,如果尝试从中读取值,则会引发错误?

2) can someone explain it to me, why cpp designers didn't want to simply initialize the built-in types int and int[] to zero? 2)有人可以向我解释一下,为什么cpp设计人员不想简单地将内置类型int和int []初始化为零?

No, no error will be thrown. 不,不会抛出任何错误。 You will have a mild case of undefined behaviour. 您会有轻微的不确定行为。 However, as integers don't have trap values, there is no way the behaviour can be detected. 但是,由于整数没有陷阱值,因此无法检测到该行为。

Note that the C++ language itself throws exceptions only very, very rarely - about the only times I can think of where it does it is when performing an invalid cast to a reference via dynamic_cast , and when new fails to allocate. 请注意,C ++语言本身仅非常非常罕见地引发异常-大约只有我能想到的一次,它是通过dynamic_cast对引用执行无效的转换时以及在new分配失败时发生异常的地方。 Of course the Standard Library may throw in a number of error conditions. 当然,标准库可能会引发许多错误情况。

As to why C++ works that way (and C too), well initialisation takes time, and if it is not needed that is time wasted. 至于为什么C ++如此工作(以及C也是如此),那么初始化很费时间,如果不需要它,那会浪费时间。 For example, if you were going to read user input immediately into those variables, there is little point in initialising them before you do so. 例如,如果您打算立即将用户输入读入这些变量,那么在进行初始化之前没有什么意义。

An error won't be thrown, this isn't Java. 不会引发错误,这不是Java。 :) :)

What will be returned will be whatever happens to be in the memory at that time. 返回的内容将是那时内存中发生的任何事情。 What you have is an "uninitialized" variable. 您拥有的是一个“未初始化”变量。 It might be 0. It might be 42. It will be something unpredictable every time. 可能为0。可能为42。每次都将是不可预测的。

Moral of the story - initialize ALL of your variables. 故事的寓意-初始化所有变量。 Failure to do so can cause incredibly difficult bugs down the road. 否则可能会导致难以置信的错误。

Since this is C++, use a default constructor to initialize your struct: 由于这是C ++,请使用默认构造函数初始化您的结构:

struct Tables { 
       int i; 
       int vi[10]; 
       Table t1; 
       Table vt[10]; 

Tables() {
  i = 0;
  for (int iter = 0; iter < 10; iter++)
    vi[iter] = 0;
}

}; 

Tables tt;

Since they are not pointer types, they will be populated with whatever is on the stack at that location. 由于它们不是指针类型,因此将使用该位置堆栈上的内容填充它们。 If they were pointer types and you dereferenced them without properly initializing them, then yes you would have issues. 如果它们是指针类型,并且您在未正确初始化它们的情况下取消了对它们的引用,则是的,您将遇到问题。

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

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