Odd behavior when initializing an array using a class member
I found an oddity with initialization that I'm trying to wrap my head
around. I understand that an array can be initialized to all default
values by using T[size] {}, which seems to work in most cases, but doesn't
seem to work when being initialized by a class member, as in:
class Hashtable {
int *table;
int size;
public:
Hashtable() : size { 10 }, table { new int[size]{} } {
for (int index = 0; index != size; ++index) { // size = 10
cout << table[index] << endl; // this displays an
uninitialized array??
}
}
};
If I try to initialize table with a literal, as in table { new int[10] {}
}, all the items in the array are set to default (0). Why is that?
No comments:
Post a Comment