Aren't Boolean variables always false by default?

前端 未结 5 459
花落未央
花落未央 2021-02-01 12:48

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),

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 13:25

    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 a default value of zero if they are not explicitly initialized.

    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.

提交回复
热议问题