+ 3
As there is less response from community on this question, i have a feeling now that this asked feature is less used... First of all , I would not go with arr[] at all... If size is not known, I would choose vector or other stl which is flexible with varying size... Even with arr[] and you are declaring same inside class initialization, it is good to give size upfront like arr[10] Regarding arr[] not initializing in class declaration itself and choosing constructor definition , i have not tried that ever.... I just choose vector in that case. I believe constructor is known and you dont have query to use constructor for assigning member variables.. if so, will be happy to help... But not sure how to use constructor to assing arr[] Still you question is valid but i dont know what is exact answer and why compiler restricts so throwing error...
12th Jul 2022, 10:14 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 2
Yeah i am not sure about answer Regarding the question you have about constructor, i meant to say that arr is class member Class member should be initialized in constructor You can initialize in class itself, but i prefer to use constructor for this purpose.... static member in class is initialized using inline is good but not for non static members
11th Jul 2022, 8:44 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Because you need to allocate memory for the array pointer to point. Otherwise you get a dangling pointer.
11th Jul 2022, 7:08 AM
Mustafa A
Mustafa A - avatar
+ 1
Personally , I am not in favour of in class initialization of non static data members One should go with constructor only for same Still if you are interested , I can think of the error is as below Simply it is not supported and can be supported easily in future... Why not supported is my guess as below. Sizeof done on class name has to decide how many elements are there based on initialization of arr Sizeof should depend on declaration and not initialization hence this is not supported arr[10] can be easily computed without checking value when one does sizeof (myClass) This is just my guess and personally I am not in favour of non static data members initialization in class itself Let's wait for others from community to respond on this
11th Jul 2022, 8:24 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
+ 1
Sounds good... plz share here also if you find something as I am also curious to know this
12th Jul 2022, 11:43 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
Manav Roy I am not sure.
11th Jul 2022, 7:44 AM
Mustafa A
Mustafa A - avatar