E
E
Eugene Ordinary2016-12-15 11:28:19
Programming
Eugene Ordinary, 2016-12-15 11:28:19

Is it possible to initiate class components like this?

struct TList
{
  byte n = 0;
  int M[256];
  void Reset() { n = 0; };
  void Push(int x) { M[n] = x; n++;  }
}

This means byte n = 0. Will it always be n=0 when declaring a variable of type TList?
I know that it is possible to write a constructor TList() {n=0;} or TList() : n(0) {}. Just wondering if such a short version is acceptable.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Shatunov, 2016-12-15
@evgeniy8086

en.cppreference.com/w/cpp/language/class Section
"Member specification".
If you are using the c++11 standard and above, then the initialization of the fields is immutable.
The only exception would be a field initialization list in a custom constructor.
en.cppreference.com/w/cpp/language/initializer_list

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question