c++ - Why marking variables as a constant? -
possible duplicate:
does declaring c++ variables const or hurt performance?
besides point of can't change cost variables, use less memory or can access values faster?
const int = 1; int b = 1;
taking account it's same global, local , class member.
thanks
does use less memory or can access values faster?
usually neither. makes program more robust because (or other people) cannot change value accidentally.
this makes sense in public apis when want show consumers won’t modify values. every modification of variable means change in program state. in order programmers sure program works correctly need keep track of state, , tremendously more difficult if don’t know when or how variables changed.
the primary purpose of const
therefore documentation of semantics , powerful purpose. use const
often.
Comments
Post a Comment