-2

I have recently come across a constructor that looked like this:

classname::classname(index_t m, index_t n) : m(m), n(n) {...}

However, I am not sure what : m(m), n(n) does. I think it assigns the parameters index_t m, index_t n to local variables of the same type and name - would this observation be correct?

Thank you!

Sam
  • 207
  • 1
  • 4
  • 12

1 Answers1

1

It's called initialization list and it's used to initialize the non-static members. You usually use them if you want to avoid double initialization of the members (if you assign the member in the body constructor, the memebers alredy exist, so you are "doubling" the init process).

You can find more info on the reference: https://en.cppreference.com/w/cpp/language/constructor

Simone-Cu
  • 1,109
  • 8
  • 21