There are several ways to initialize const elements within a class.
Defining a const element in the general case also requires variable initialization.
1) Inside the class, if you want to initialize const, the syntax is similar to this
static const int a = 10;
2) The second method may be
class A { static const int a;
3) Well, if you do not want to initialize when declaring, then the other way is through the constructor, the variable must be initialized in the initialization list (not in the body of the constructor). It should be like that.
class A { const int b; A(int c) : b(c) {}
ravs2627 Jun 25 '14 at 3:56 a.m. 2014-06-25 03:56
source share