+ 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

23rd Apr 2018, 2:19 PM
Jacob
9 Answers
+ 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
23rd Apr 2018, 5:09 PM
šŸŒ›DTšŸŒœ
šŸŒ›DTšŸŒœ - avatar
+ 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
23rd Apr 2018, 3:57 PM
į Œį ŒCode X
į Œį ŒCode X - avatar
+ 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.
23rd Apr 2018, 4:21 PM
aklex
aklex - avatar
+ 1
alright thanks
23rd Apr 2018, 6:18 PM
Jacob
+ 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.
26th Apr 2018, 3:56 AM
Luminant
Luminant - avatar
0
yes I know the thing is I canā€™t think of a single scenario where this would actually be helpful
23rd Apr 2018, 2:23 PM
Jacob
0
I disagree a height has the possibility to change
23rd Apr 2018, 2:27 PM
Jacob
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.
23rd Apr 2018, 4:18 PM
Adam
Adam - avatar
- 1
const pi = 3.14 setting height of a person for calculations there are a thousand uses for it.
23rd Apr 2018, 2:24 PM
Adam
Adam - avatar