2

This is a quick question, Probably has already been answered somewhere but I can't find anything... I might just be blind.

Is there any meaningful differences between making a default constructor like this:

Complex(){
  Real = 0;
  Imag = 0;
}

or like this:

Complex(): Real(0), Imag(0){}

or is it just a simple difference in syntax?

And which one is more prevalent and is usually used, or it doesn't matter and people use any? Thank you!

Cewu00
  • 138
  • 9
  • 2
    Read https://en.cppreference.com/w/cpp/language/constructor – Tony Tannous Nov 07 '20 at 19:15
  • 1
    @TonyTannous I will check it out. Thanks :) – Cewu00 Nov 07 '20 at 19:18
  • 3
    this is a topic that is apparently completely underestimated in teaching. There is not just a small difference between the two and often it is essential to use the right one – 463035818_is_not_an_ai Nov 07 '20 at 19:26
  • 1
    All members and base classes must be constructed before entering the body of the constructor. If the constructor of the member does anything, the Member Initializer List will run that constructor before entering. If you don't use the initializer list, the object will be default constructed (if it's available), then in the body the program could construct another instance and assign it to the member resulting in a performance hit. Compilers are smart these days, so if it can prove there are no missed side effects if it replaces the two constructions and assignment, it may replace. – user4581301 Nov 07 '20 at 19:27

0 Answers0