Const member in class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Const member in class

Hi everyone. I have two constructors in my class : - first has no parameters; -second is initialising const variable; And that lead to have an error like” my class provides no initialiser for const member”. So I can’t create non const object and I don’t have an access to non const methods. Thank you

14th Dec 2018, 8:10 PM
Nazar
2 Answers
+ 2
Uninitialized constant member variables are not allowed. Therefore you need to make sure that you initialize your const member in every constructor that you write, even if it is just a default value like 0. Otherwise there is a possibility that the first constructor with no parameters is called and the const member ends up not initialized. Here is a rather similar problem: https://stackoverflow.com/questions/4343934/uninitialized-constant-members-in-classes
14th Dec 2018, 9:28 PM
Shadow
Shadow - avatar
0
Thanks for explainings .
15th Dec 2018, 8:58 AM
Nazar