#include <iostream>
class X {
public:
int a;
};
int main(int argc, char *argv[]) {
X x;
std::cout<<x.a<<std::endl;
}
why is 2130567168? not 0?
#include <iostream>
class X {
public:
int a;
};
int main(int argc, char *argv[]) {
X x;
std::cout<<x.a<<std::endl;
}
why is 2130567168? not 0?
It could be anything. Since x.a
is uninitialized it's value is Indeterminate.
In C++, class members are not default initialized. They need explicit initialization, in the absence of any they remain uninitialized.
Using any such uninitialized class members gives your program only one thing, Undefined Behavior.
You need to initialize x.a
to something meaningful, using the Member Initializer list.
class X
{
public:
int a;
X(int i):a(i){}
X():a(0){}
};