Zero Initialization
C++ by default does not initialize automatic variables. For the fundamental types (such as int, double, or pointer types and “Plain Old Data” (POD)), there is no default constructor that initializes them with some default value. Consequently, uninitialized auto variables and aggregates have undefined value.
int x; // x has undefined value |
The auto variables can be zero-initialized by instantiating them with syntax of the default constructor.
int x = int(); // x has ‘0’ value |
For the same reason it is recommended to use zero-initialization syntax in template code like as below:
template <typename T> void MyFunction() { T x = T(); // x is zero (or false) if ‘T’ is a built-in type } |
Zero Initialization
Reviewed by Sourabh Soni
on
Monday, February 01, 2010
Rating:
No comments