0

I don't understand when I need to write myself the "initializer list" in constructors? I reed about it in some tutorials but it's still not clear how to work with this.

(I understood that the fields of class must be initialized before the execution of the function body. But some details are missing and it's not clear when can I need to write the "initializer list" myself, and when I can to trust the "hidden initializer list" that executed by the default constructor of any field)

Software_t
  • 576
  • 1
  • 4
  • 13
  • @Ron Ok, I meant to ask, when I need to write it at all (cases that it's "must" and I can't trust the default constructor of any field). – Software_t Mar 13 '18 at 20:18
  • 1
    For classes with private members, you always need to write it - classes don't magically initialise themselves. –  Mar 13 '18 at 20:19
  • You want us to enumerate all of those cases? – StoryTeller - Unslander Monica Mar 13 '18 at 20:20
  • @StoryTeller No, Of course. But to get a some rules about it and by this get the instinct.. – Software_t Mar 13 '18 at 20:21
  • @StoryTeller In fact, now, I see (in the related link that "Drew Dormann" added ) this rule: `if a class doesn't have a default constructor, or you have a const member variable, you must use an initializer list` (This is can be a good answer [I don't know if it's a full answer or not , but anyway it's give a some instinct] for my question) – Software_t Mar 13 '18 at 20:24
  • @NeilButterworth Are you sure that it's correct? if to this private members exist a default constructor? – Software_t Mar 13 '18 at 20:29
  • Yes, if there is a default constructor for those types, they will be initialised without you doing anything. But many (most?) types don't have a default constructor. –  Mar 13 '18 at 20:31
  • @NeilButterworth Right, ok, thank you for your help! – Software_t Mar 13 '18 at 20:33

0 Answers0