The code is like
class C{
public:
int m1;
int m2;
C(int m);
}
C::C(int m):m1(m){};
int main(){
C* c = new C(1);
cout << c->m2 << endl;
}
I want to know what value m2 is to be initilized. I think c is value initialized, and m2 is default initialized.
I test it with C++11 and g++4.8.4, and m2 seems always 0. I think 0 is default initializing, but default initializing is not 0. So initializing to 0 can be guaranteed?
c
is copy initialized, and not value initialized. m2
is in fact default initialized, yes, but that does not mean that its value is always 0
(that would be guaranteed by value and aggregate initialization).
int(); // value initialized - always 0
int{}; // value initialized - always 0
int a; // default initialized - indeterminate value
struct X {};
X x{}; // aggregate initialized
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With