简体   繁体   English

在C ++类中,是否必须初始化数组的成员变量?

[英]In a C++ class, do you have to initialize member variables that are arrays?

For example: 例如:

class Cls
{
private:
    int x;
    int y;
    int arr[10];

public:
    Cls();
};

Cls::Cls() : x(0), y(0) {}

My understanding is that an uninitialized array will have garbage values. 我的理解是,未初始化的数组将具有垃圾值。 As long as I don't write code that expects the array to have a specific value before filling it, is there any issue with not initializing the array? 只要我不编写期望数组在填充之前具有特定值的代码,不初始化数组是否存在任何问题? Or do I need something like this as the constructor: 还是我需要像这样的东西作为构造函数:

Cls::Cls() : x(0), y(0), arr() {}

The constructor of Cls is not required to initialize the array member. Cls的构造函数不需要初始化数组成员。 Failing to initialize it will leave the elements with indeterminate values. 初始化失败将使元素具有不确定的值。 Contrary to popular belief, objects with indeterminate values aren't merely objects whose values are unspecified; 与流行的看法相反,具有不确定值的对象不仅仅是具有未指定值的对象;它还具有一些不确定性。 as a matter of fact, attempting to read them, before first writing definite values to them, causes undefined behaviour. 实际上,在先向它们写入确定的值之前尝试读取它们会导致未定义的行为。 For this reason, it's generally advisable to set the values as soon as possible. 因此,通常建议尽快设置这些值。

If your logic is that you definitely won't write code that accesses them before setting them, then no you don't need to initialise them. 如果您的逻辑是您肯定不会在设置它们之前编写访问它们的代码,那么您无需初始化它们。 If for instance you straight up did not use the array at all, you could simply leave them unitialised. 例如,如果您直接不使用任何数组,则可以将它们统一化。

However it is simply best to initialise them since there's no reason not to, even if you initialise them to bad values. 但是,最好将它们初始化,因为没有理由不这样做,即使将它们初始化为错误的值也是如此。 What might happen is although you always write before reading now , that may change in the future. 什么可能发生的情况是,虽然你现在总是读前写的,可能在未来改变。 6 months down the track someone may refactor something, and now a read happens before a write. 走了6个月之后,有人可能会重构某些东西,现在在写之前先进行读取。 Because that's undefined behaviour, maybe your program will seem to function correctly, or it might work on their machine but crash on another. 因为这是未定义的行为,所以您的程序似乎可以正确运行,或者可以在其计算机上运行,​​但在另一台计算机上崩溃。 If you initialise them, you just won't get these problems. 如果初始化它们,将不会遇到这些问题。

Perhaps your concern is that 0 is a valid value and you don't want your program to accidentally get that value, you'd rather make it clear it's not properly initialised, but with undefined behaviour you could get 0 or some other valid value or a crash, and it may be inconsistent. 也许您担心的是0是一个有效值,并且您不希望您的程序意外地获得该值,您宁愿明确表示它没有正确初始化,但是如果使用未定义的行为,您可能会得到0或其他有效值,或者崩溃,并且可能不一致。 You will probably thank yourself if you just initialise everything properly. 如果您正确初始化所有内容,您可能会感谢自己。

The C++ police or a compiler won't stop you from doing so, and your program can still be well formed, but you may as well guarantee it by just initialising them. C ++警察或编译器不会阻止您这样做,并且您的程序仍然可以很好地构成,但是您也可以通过初始化它们来保证它。

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

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