I have:
class SomeObject {
public:
SomeObject() { ... }
// Other fields and methods
};
class anOtherObject {
private:
SomeObject array[SOME_FIXED_SIZE];
public:
anOtherObject() : ... { ... }
};
My question is - what does array contain when and after the constructor is called? should I initialize it by myself with a for loop or does the compiler call the default constructor for each array[i] , 0<=i<SOME_FIXED_SIZE
?
The array is default-initialized, which means its elements are default initialized one by one. Since your array holds user defined types, this means their default constructor will be called. If your array held built-in types or PODs, you would have to be explicit and value-initialize it, since default initialization would mean no initialization is performed on the elements:
anOtherObject() : array() {}
// ^^^^^^^ value-initialize array
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