What's problem with this

Hi! I want a little help with constant. What is the problem if I don't use it. Please help me with this gaming example:

const int KillBonus = 5000;

even if I don't use the "const" in it. It would have given the value 5000 then what is the need of using "const".
const keyword signifies that the variable KillBonus cannot be changed during the execution of program, if an attempt is made to do so the compiler will throw an error.
closed account (zb0S216C)
rckinesis wrote:
"What is the problem if I don't use it."

A smart compiler will optimise it way, which basically means the compiler will simply discard the variable. However, KillBonus will not allocate any memory in this case, so you would not notice its absence.

rckinesis wrote:
"what is the need of using "const"."
const is an abbreviation for "constant". As its name implies, it protects a variable from modification. Once a constant variable has been declared, its value cannot change, unless you screw the compiler to work around it.

const can be used for optimisation purposes. In computers, resides ROM (Read-Only Memory). This is an area of memory which can only be read from. If a variable is declared constant, the compiler can place it in ROM, but it doesn't have to. ROM memory is relatively quick as a result.

My rule of thumb is this: "If it never needs to change, declare it constant."

Wazzak
Last edited on
Topic archived. No new replies allowed.