C++ 默认情况下,布尔变量并不总是假的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4622225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Boolean variables aren't always false by default?
提问by Nav
I had declared a boolean variable bool abc;
in a class and thought that it would be false by default. An if condition in my program if (abc)
turned out to be true, so I output the value of abc, and saw that it contained the value 55. Is this normal? Do we always have to assign 'bool abc=false' to be sure that it is false?
我bool abc;
在一个类中声明了一个布尔变量,并认为默认情况下它是假的。我的程序中的一个 if 条件if (abc)
结果为真,所以我输出 abc 的值,看到它包含值 55。这正常吗?我们是否总是必须分配 'bool abc=false' 以确保它是假的?
采纳答案by Edward Strange
Yes, you should always initialize your variables. Until you intimately learn the times when it is and isn't necessary to do so explicitly, you should do it all the time, no matter what. And by then...well...why stop a good habit?
是的,您应该始终初始化您的变量。除非您深入了解需要和不需要明确这样做的时间,否则无论如何都应该一直这样做。到那时......好吧......为什么要停止一个好习惯?
To initialize a bool to false it's sufficient to default construct it:
要将 bool 初始化为 false,默认构造它就足够了:
struct X
{
bool b;
X() : b() {}
};
回答by Nylon Smile
Talking about primitive built-in data types(bool, char, wchar_t, short, int, long, float, double, long double), according to C++ standard, only global variables get default value of zeroif they are not explicitly initialized.
谈到原始的内置数据类型(bool、char、wchar_t、short、int、long、float、double、long double),根据 C++ 标准,只有全局变量在没有显式初始化的情况下才会获得默认值零。
For local variables it's not required for the complier to clean up the content of the memory they are assigned to. A local variable -- if not explicitly initialized -- will contain an arbitrary value.
对于局部变量,编译器不需要清理分配给它们的内存的内容。局部变量——如果没有明确初始化——将包含一个任意值。
回答by Regan Koopmans
Only global variables are assigned 0 (false) by default, any local variables are given a non-zero garbage value, which would evaluate to true in a boolean variable.
默认情况下,只有全局变量被分配为 0(假),任何局部变量都被赋予一个非零垃圾值,这在布尔变量中将评估为真。
回答by KitsuneYMG
回答by user541686
Yes. ALWAYSinitialize your variables before use. Even if the language guarantees that they will have specific values. If you can't force yourself, get a compiler that will complain, and then make yourself do that. :)
是的。始终在使用前初始化您的变量。即使语言保证它们将具有特定的值。如果你不能强迫自己,找一个会抱怨的编译器,然后让自己这样做。:)
However, don'tinitialize values unless it really has a meaning for them to be initialized. For example, if you have a loop like this (I'm not saying this is good code, it's just an example):
但是,不要初始化值,除非它确实具有要初始化的意义。例如,如果你有一个这样的循环(我不是说这是好的代码,这只是一个例子):
int i = 0;
while ((i = getNum()) == 5)
{
}
Don't initialize i
to zero like I did. It makes no sense, and while it shuts up the compiler, it introduces the possibility that you'll forget it and then your code will be messed up. If you can force yourself to initialize only at the right times -- no more, no less -- then you'll make debugging a heck of a lot easier, since your wrong code will lookwrong even at just a glance.
不要i
像我一样初始化为零。这是没有意义的,虽然它关闭了编译器,但它可能会导致您忘记它,然后您的代码就会被搞砸。如果你能强迫自己只在正确的时间初始化——不多也不少——那么你会让调试变得容易很多,因为你的错误代码即使只是一眼就看起来是错误的。
So, in one line: Never initialize just to prevent the compiler from complaining, but always initialize before use.
所以,在一行中:永远不要仅仅为了防止编译器抱怨而进行初始化,而是始终在使用前进行初始化。