Const Variables in a Class | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Const Variables in a Class

So I was just messing around with some code and wanted to declare an array of strings that I already new the values for, like this: class Player { //... public: static const char* NAMES[]; //... }; const char* Player::NAMES[] = {"Bob", "Billy", "Joe"}; Then I noticed I could probably declare other data variables const as well. At least the ones that weren't going to be changed ever in the code. Like this: public: unsigned int maxHealth; // I changed it to "const unsigned int maxHealth;" I've read a ton of stuff on c++ and practiced a bunch, so I might have just forgotten this rule, if it even is a rule: For the variables I've defined like 'maxHealth', can their value only be assigned within the constructor, since I don't want them to be static??

31st May 2017, 11:30 PM
Zeke Williams
Zeke Williams - avatar
2 Answers
+ 3
Perfect article for my question. Thanks a bunch Ulisses!
1st Jun 2017, 12:02 AM
Zeke Williams
Zeke Williams - avatar