2

I am familiar with this approach, I just need to know what this approach is called.

class MyClass {
    int A, B;
public:
    MyClass(int a, int b) : a(A), b(B) {}
};

I am keen to know the name of this approach of initialization of class variables directly in the function argument list line.

If I am missing something in this understanding, please highlight that as well.

Pranjal Kaler
  • 483
  • 3
  • 14
  • 4
    You are leveraging [the _member initializer list_](https://en.cppreference.com/w/cpp/language/constructor) in the declaration of your constructor (and your program is ill-formed; the correct syntax is `S(parameter-list) : data_member_{init-expression-which-may-use-parameters-from-parameter-list}, another_data_member_{...}, ... {}`). – dfrib Mar 05 '21 at 13:26
  • 4
    A detail: it should be `A(a), B(b)` – Damien Mar 05 '21 at 13:26
  • its a bit strange that this is so widely unknown among starters, because it is rather essential. But don't worry, I had been writing c++ for long time before I knew what the member initializer list is – 463035818_is_not_an_ai Mar 05 '21 at 13:54

0 Answers0