Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does this zero initialize the data item?

I find myself confused over the rules for zero initialization in c++. With this code is data_ initialized to zero? I believe it should be, and looking at the generated assembly code with my compiler it is, but I know that's no guarentee it's required.

#include <iostream>

class test
{
public:
    test(); 
    int data_;
};


// Does this zero initialize data_ ?
test::test() : data_()
{
}

int main()
{
    test t;
    std:: cout << t.data_;
}
like image 252
jcoder Avatar asked Nov 22 '25 16:11

jcoder


1 Answers

Yes: data_() denotes value initialization, and for fundamental types, value initialization is zero initialization, i.e. data_ will start out with value 0.

like image 104
Kerrek SB Avatar answered Nov 25 '25 06:11

Kerrek SB