简体   繁体   English

如果没有初始化类字段,为什么在c ++中没有编译时错误?

[英]why in c++ there are no compile-time error if class field is not initialized?

In my class I was using such field: 在我的课堂上,我使用了这样的字段:

private:
bool firstSeqNumReceived;

Everything were working fine for a while but after one commit I've figured out that now field is true by default. 一切都运行了一段时间,但是一次提交后,我发现默认情况下now字段为true Surprisingly field is not initialized to false by default, instead assigned value is implementation dependent (refer to What is the default value for C++ class members for more details ) 令人惊讶的是,默认情况下,字段未初始化为false ,而是分配的值取决于实现(有关更多详细信息,请参阅C ++类成员的默认值是什么

Now I wonder why compiler doesn't produce compile-time error forcing me to add initialization? 现在,我想知道为什么编译器不会产生强制我添加初始化的编译时错误? Who needs "implementation-dependent" default value, are there any use-cases? 谁需要“与实现有关”的默认值,是否有用例? Why not produce compile-time error in this case? 在这种情况下为什么不产生编译时错误?

I wonder why compiler doesn't produce compile-time error forcing me to add initialization? 我想知道为什么编译器不会产生强制我添加初始化的编译时错误?

Because the compiler assumes you know what you're doing. 因为编译器假定您知道自己在做什么。

In C++ you don't pay for what you don't use. 在C ++中,您不用为不使用的东西付费。 There may be use cases where initialization is a waste of time. 在某些情况下,初始化会浪费时间。 For example, in a class where real meaningful values for members cannot possibly be computed until after the object has been constructed, initializing the members to some default or sentinel value accomplishes little. 例如,在一个类中,只有在构造完对象之后,才可能无法计算出对成员真正有意义的值,将成员初始化为某个默认值或前哨值的作用很小。

The use-case is basically, that it depends on the program flow if you are using the variable or not. 用例基本上是,取决于是否使用变量,它取决于程序流。 If you don't use it in parts of your code then there is also no need to zero initialise it. 如果您不在代码的某些部分中使用它,则也无需将其初始化为零。

Also if you know, that the value will be assigned in the constructor or some other member function it would be just a superfluous step to first write 0 into memory. 同样,如果您知道该值将在构造函数或其他成员函数中分配,那么将0首先写入内存只是多余的步骤。

The standard specifies that it is undefined behaviour to access an uninitialized object. 该标准指定访问未初始化的对象是未定义的行为。 There are a few reasons such undefined behaviour appears in the standard. 在标准中出现此类未定义行为的原因有很多。 This one is more about not giving you more than you ask for. 这更多的是不给您超出要求的范围。 Sometimes you really don't want to initialize a variable as soon as you create it. 有时,您确实不想在创建变量后立即对其进行初始化。 Instead, the compiler trusts you to initialize an object when you need it initialized. 而是,编译器信任您在需要初始化对象时对其进行初始化。 As an example of when you don't want it initialized automatically: 例如,当您不想自动初始化时:

int x;
std::cin >> x;

It would be completely pointless to automatically initialize x to 0 as the value is immediately overwritten. 自动将x初始化为0是完全没有意义的,因为该值会立即被覆盖。 The power to initialize a variable is in your capable hands. 初始化变量的能力掌握在您的手中。

Compilers may also assume you do not invoke undefined behaviour in order to make optimizations. 编译器还可能假设您没有为了进行优化而调用未定义的行为。

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

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