If I have a class like the below one:
class foo {
private:
int a;
public:
foo (int arg1) : a(arg1) {}
};
The attribute a will be default-initialized in the line it got declared.
Now, when the parameterized constructor is called, it gets re-initialized to the value arg1 in the member-initialization list.
- Is my understanding correct that
agets re-initialized in the member initialization list? - If yes, what does it mean by an attribute getting re-initialized (Initialization means memory getting allocated. Now, what does re-initialization mean?)?
No,
awon't be "re-initialized", i.e. initialized twice. When the parameterized constructor is usedawill only be direct-initialized from the constructor parameterarg1, no default-initialization happens.If
foohas another constructor which doesn't initializeain member-init-list, then it'll be default-initialized. E.g.