C++ 中未设置布尔值的默认值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7863956/
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
Default value of an unset boolean in C++?
提问by Lemmons
Possible Duplicate:
Why is a C++ bool var true by default?
Say I were to do something like this:
假设我要做这样的事情:
class blah
{
public:
bool exampleVar;
};
blah exampleArray[4];
exampleArray[1].exampleVar = true;
In exampleArray, there are now 3 unset instances of exampleVar, what are their default values without me setting them?
在 exampleArray 中,现在有 3 个未设置的 exampleVar 实例,如果没有我设置它们,它们的默认值是什么?
回答by Praetorian
The default value depends on the scope that exampleArray
is declared in. If it is local to a function the values will be random, whatever values those stack locations happened to be at. If it is static or declared at file scope (global) the values will be zero initialized.
默认值取决于exampleArray
声明的范围。如果它是函数的本地值,则值将是随机的,无论这些堆栈位置碰巧处于什么值。如果它是静态的或在文件范围(全局)中声明,则值将被初始化为零。
Here'sa demonstration. If you need a member variable to have a deterministic value always initialize it in the constructor.
这是一个演示。如果您需要成员变量具有确定性值,请始终在构造函数中对其进行初始化。
class blah
{
public:
blah()
: exampleVar(false)
{}
bool exampleVar;
};
EDIT:
The constructor in the above example is no longer necessary with C++11. Data members can be initialized within the class declaration itself.
编辑:
C++11 不再需要上述示例中的构造函数。数据成员可以在类声明本身内初始化。
class blah
{
public:
bool exampleVar = false;
};
This inline default value can be overridden by a user-defined constructor if desired.
如果需要,这个内联默认值可以被用户定义的构造函数覆盖。
回答by Salvatore Previti
struct x
{
bool b;
x() : b() { }
}
...
x myx;
in this case, myx.b will be false.
在这种情况下, myx.b 将是假的。
struct x
{
bool b;
}
...
x myx;
in this case, myx.b will be unpredictable, it will be the value that location of memory had before allocating myx.
在这种情况下,myx.b 将是不可预测的,它将是分配 myx 之前内存位置的值。
Since in C and C++, a false value is defined as 0 and a true value is defined as non zero, there is a bigger possibility that a random address location will contain a true value instead of a false value. Usually, in C++, sizeof(bool) is 1, it means, 8 bit. There are 1 over 255 possibilities that a random location of memory is false, and this explain why you perceived the default boolean value as true.
由于在 C 和 C++ 中,假值定义为 0,真值定义为非零,因此随机地址位置包含真值而不是假值的可能性更大。通常,在 C++ 中,sizeof(bool) 为 1,即 8 位。有超过 255 种可能性内存的随机位置为假,这解释了为什么您认为默认布尔值为真。
回答by Donald Miner
Their default values are undefined. You shouldn't depend on them being set as one thing or another and is often called "garbage".
它们的默认值未定义。你不应该依赖它们被设置为一件事或另一件事,通常被称为“垃圾”。
Depending on your compiler, it may be set to false
. But even then, you are better off setting them.
根据您的编译器,它可能设置为false
. 但即便如此,你最好设置它们。
回答by FailedDev
The default value is indeterminate. Possibly different every time you run your program. You should initialize the value to something or have another variable indicating that your private members are not initialized.
默认值是不确定的。每次运行程序时可能都不同。您应该将该值初始化为某个值,或者使用另一个变量来指示您的私有成员未初始化。
回答by Brian Roach
Indeterminate.
不定。
Non-static member variables need to be initialized unless you can guarantee the first operation performed on them will be a write. The most common way would be via the constructor. Use an initialization list if you still want a no-arg / no-op constructor:
非静态成员变量需要初始化,除非您可以保证对它们执行的第一个操作是写操作。最常见的方法是通过构造函数。如果您仍然需要无参数/无操作构造函数,请使用初始化列表:
public:
blah() : exampleVar(false) {}
回答by Martin York
@Praetorian: Covered the main points in his answer.
@Praetorian:在他的回答中涵盖了要点。
But it is also worth noting.
但也值得注意。
blah exampleArray[4]; // static storage duration will be zero-initialized
// ie POD member exampleVar will be false.
void foo()
{
blah exampleArray1[4]; // automatic storage duration will be untouched.
// Though this is a class type it only has a compiler
// generated constructor. When it is called this way
// it performs default-construction and POD members
// have indeterminate values
blah exampleArray2[4] = {}; // Can force zero-in initialization;
// This is list-initialization.
// This forces value-initialization of each member.
// If this is a class with a compiler generated constrcutor
// Then each member is zero-initialized
// following through to non array types.
blah tmp1; // Default initialized: Member is indeterminate
blah tmp2 = blah(); // Zero Initialized: Members is false
// Unfortunately does not work as expected
blah tmp3(); // Most beginners think this is zero initialization.
// Unfortunately it is a forward function declaration.
// You must use tmp2 version to get the zero-initialization
}
回答by ObscureRobot
Unassigned default values are undefined in C/C++. If you need a specific value, then create a constructor for class blah and set your default value.
未分配的默认值在 C/C++ 中未定义。如果你需要一个特定的值,那么为类 blah 创建一个构造函数并设置你的默认值。