If I have a class defined and one of the data members is a double (for example), what is the value of that double when an object of that class is instantiated? Is it zero, or do I need to explicitly set it to zero?
TIA Fred Emmerich
If I have a class defined and one of the data members is a double (for example), what is the value of that double when an object of that class is instantiated? Is it zero, or do I need to explicitly set it to zero?
TIA Fred Emmerich
The variable is uninitialized and can be anything. Using it is undefined behavior. You would have to initialize it yourself, see also https://en.cppreference.com/book/uninitialized
EDIT: The answers to the following related questions are a good read:
Generally, the variable is uninitialized, except for the variable being static
. In this case, the variable is zero initialized, when there is no constant initialization.
Zero-initialization is performed [...] For every named variable with static or thread-local (since C++11) storage duration that is not subject to constant initialization, before any other initialization.
https://en.cppreference.com/w/cpp/language/zero_initialization
Nevertheless, I think initializing variables is good practice.