Reputation: 1203
Why in the following non-POD class the x is initalized to zero?
class test {
public:
void print() {
cout << x << endl;
}
private:
int x;
};
int main(int argc, char** argv)
{
test * tst = new test();
tst->print();
cout << is_pod<test>::value << endl;
}
Both tst->print() and is_pod() returns 0
Upvotes: 3
Views: 203
Reputation: 26302
This is a result of value-initialization of a class without a user-provided constructor.
In this case, T()
and new T()
perform zero-initialization first:
if
T
is a class type with a default constructor that is neither user-provided nor deleted (that is, it may be a class with an implicitly-defined or defaulted default constructor), the object is zero-initialized and then it is default-initialized if it has a non-trivial default constructor;
The effects of zero-initialization are:
if
T
is an non-union class type, all base classes and non-static data members are zero-initialized, and all padding is initialized to zero bits. The constructors, if any, are ignored.
and
if
T
is a scalar type, the object's initial value is the integral constant zero explicitly converted toT
.
Upvotes: 6