+ 1

[C++ Constant] Can constants be declared while the program is running?

I see that constant variables have to be initialized at the time of their creation and they also cannot be changed while the program is running. (Almost similar to a Python's tuple). However, can it be declared when the program is running? I.E.: "int my_func { const int x = 42; return x; } int main { cout << my_func() << endl; }

7th Jan 2018, 7:33 AM
Jacob Yuhas
Jacob Yuhas - avatar
2 ответов
+ 14
You may. The constant will be local to my_func, and will be re-declared and initialised every time the function is called. This is similar to declaring a constant in main, wherein the constant will be local to main.
7th Jan 2018, 7:41 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Thank you Hatsy, or I should say "Haste-ey" for the swift response xD
7th Jan 2018, 8:26 AM
Jacob Yuhas
Jacob Yuhas - avatar