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
a
gets 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,
a
won't be "re-initialized", i.e. initialized twice. When the parameterized constructor is useda
will only be direct-initialized from the constructor parameterarg1
, no default-initialization happens.If
foo
has another constructor which doesn't initializea
in member-init-list, then it'll be default-initialized. E.g.