What are the advantages of const in C++ (and C) for the uninitiated?
Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.
The big advantage of const over #define is type checking. #defines can't be type checked, so this can cause problems when trying to determine the data type. If the variable is, instead, a constant then we can grab the type of the data that is stored in that constant variable.
Constants can make your program more readable. For example, you can declare: Const PI = 3.141592654. Then, within the body of your program, you can make calculations that have something to do with a circle. Constants can make your program more readable.
The const keyword specifies that a variable's value is constant and tells the compiler to prevent the programmer from modifying it. In C, constant values default to external linkage, so they can appear only in source files.
The big advantage of const over #define is type checking. #defines can’t be type checked, so this can cause problems when trying to determine the data type. If the variable is, instead, a constant then we can grab the type of the data that is stored in that constant variable. Since const are considered variables, we can use pointers on them.
C const In C const is the keyword to create constants (variables which don’t change their value). Normally the usage of const is straightforward, but it becomes tricky when used with pointers. 30% Off my course, 30 days money back guarantee!
The big advantage of const over #define is type checking. We can also have poitners to const varaibles, we can pass them around, typecast them and any other thing that can be done with a normal variable. One disadvantage that one could think of is extra space for variable which is immaterial due to optimizations done by compilers.
Whenever const keyword is attached with any method (), variable, pointer variable, and with the object of a class it prevents that specific object/method ()/variable to modify its data items value. There are a certain set of rules for the declaration and initialization of the constant variables:
Const is particularly useful with pointers or references passed to a function--it's an instantly understandable "API contract" of sorts that the function won't change the passed object.
See also: http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.4
When used as a const reference in a function, it lets the caller know that the thing being passed in won't be modified.
void f(Foo &foo, const Bar &bar) { ... }
In this case the caller will know that the foo
might be modified, but the bar
will not. The compiler will enforce this when compiling the body of f()
, so that bar
is never modified and never passed on to another function that might modify it.
All of the above safeguards can be bypassed using const_cast
, which is why such a cast is considered "dangerous" (or at the very least, suspicious).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With