Default value of an unset boolean in C++? [duplicate]

最后都变了- 提交于 2019-12-08 14:35:32

问题


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?


回答1:


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.

Here's a 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.

class blah
{
  public:
  bool exampleVar = false;
};

This inline default value can be overridden by a user-defined constructor if desired.




回答2:


struct x
{
    bool b;

    x() : b() { }
}

...
x myx;

in this case, myx.b will be false.

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.

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.




回答3:


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.




回答4:


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) {}



回答5:


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.




回答6:


@Praetorian: Covered the main points in his answer.

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 
}



回答7:


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.



来源:https://stackoverflow.com/questions/7863956/default-value-of-an-unset-boolean-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!