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?
The default value depends on the scope that
exampleArrayis 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.
EDIT:
The constructor in the above example is no longer necessary with C++11. Data members can be initialized within the class declaration itself.
This inline default value can be overridden by a user-defined constructor if desired.