2

Consider this copy constructor:

myclass::myclass(const myclass& rhs):
  _a(rhs._a), _b() {
}

Assuming _b is defined as:

int _b[100];

What would _b get initialized to? What is the difference between writing _b() vs not including _b in the list at all?

Ari
  • 7,251
  • 11
  • 40
  • 70
  • Related if not dupe: https://stackoverflow.com/questions/1613341/what-do-the-following-phrases-mean-in-c-zero-default-and-value-initializat – Rakete1111 Oct 16 '17 at 17:40

1 Answers1

4

_b() assures that the array _b will be zero-initialized, i.e. all its elements will be zero.

If you don't include _b() in the constructor initializer list, then _b will not be initialized (technically it is called default-initialized) and may contain anything. Using it non-initialized causes undefined behaviour.

vsoftco
  • 55,410
  • 12
  • 139
  • 252