0

Just wondering, what is the difference between these initialization in classes in C++

first:

Complex (): real(0), imaginary(0) { }

second:

Complex() {
   real = 0;
   imaginary = 0;
}
Anand Sharma
  • 53
  • 10

1 Answers1

0

In the first variant members are directly initialized with values (use when can). This is the only way to initialize const members. Constness has many useful side effects.

In the second variant members are first default-initialized (by the compiler generated code) then assigned values (use when cannot avoid, usually can).

bobah
  • 18,364
  • 2
  • 37
  • 70