+ 1
C++ const keyword useless?
I’ve been writing C++ code for maybe 3 or 4 months now and I still can’t find a use for const. I don’t know about you but I always have a need to change a variable. I could consider a use for it if it did not have to be declared const right on initializing but you can’t. Tell me your thoughts on const
11 Respuestas
+ 21
Jacob well u can use variables in switch case values using const ...
without const u won't be able to use variables in switch statement's case values
https://code.sololearn.com/cP9dTt00MO5v/?ref=app
+ 4
well i also say to myself if you know youre never gonna change a variables value,theres still no need for const.Just declare it and just dont change it😂.But const might be useful so as to produce an error when you accidentally change that variable.
Practical Example::
a global variable is declared as const in a code.that variable is the backbone of the code so changing its value would affect the code,fine!!but what is the need of const?that code is 1000+ lines of c++ so you cannot remember the names of all variables declared.Accidentally you declare another variable with the constants name and assign it a value thinking its a new variable.if you did not declare the previous variable has const,the system will silently change its values,and youll end up with sh*itty results in your code
+ 1
@Brains If you use constexpr on variables you know will not need to be changed during runtime, the compiler can do compile time computations and execute functions at compile time, which can drastically improve runtime performance. It is extremely useful.
+ 1
alright thanks
+ 1
For speed when compiling as compiler can make more assumptions. Same goes to passing by reference of function arguments.
Less copying/ writing, more speed.
It also helps to self restrict from accidentally changing whatever data you do not want to be changed as C++ compiler will hit you with an error if you did by accident.
Back in PIC/microprocessor chips like 8088,
defining const in C, compiler will try to store it in EPROM then in RAM as memory is limited. I’m not sure how it’s done at compiling level by that’s what I’m taught.
And EPROM is should be faster than RAM as the controller does not have to tell the memory to read or to write.
But please don’t tell others you are defining it as const so it goes into EPROM.
Personal Computers aka your PC does not work that way. PC works with RAM and cache, not EPROM which is normally found on specialised designed circuit board installed with a PIC.
0
yes I know the thing is I can’t think of a single scenario where this would actually be helpful
0
I disagree a height has the possibility to change
0
Jacob Not in the timeframes over which you are using it in the instance of a c++ variable to calculate something. The height of a person used to calculate BMI or some other medical thing ismt going to change while you are calculating it. In terms of the calculation it is a constant.
- 1
const pi = 3.14
setting height of a person for calculations
there are a thousand uses for it.