Since C++2003 we have value-initialisation as well as default-initialisation. Meaning that:
struct Foo {
int i;
std :: string s;
};
Foo f1; // f1.s is default-constructed, f1.i is uninitialised
Foo f2 = Foo (); // f1.s is default-constructed, f1.i is zero
Right?
Now suppose I have this class
class Bar : public Foo {
int n;
Foo f [SIZE];
public:
Bar ();
};
When I write the constructor for Bar
, I may want to default- or value-initialise either the parent class or the f[]
member. I think the choice for initialising the parent is simply:
Bar :: Bar () : Foo (), n (-1) {} // Parent is value-initialised (Foo::i is zero)
Bar :: Bar () : n (-1) {} // Parent is default-initialised (Foo::i is undefined)
But what about the f[]
member?
- How do I default-initialise all members?
- How do I value-initialise all members?
- If I use C++11 initializer lists, what happens if the initializer list has a different size than
SIZE
?