Main Content
What does const mean? C and C++ Archive - Originally posted on "The Horse's Mouth" - 2010-01-15 08:30:28 - Graham Ellis
If you declare a variable to be a const , you're telling the compiler that it's a read-only variable and that it won't be changed throughout its existance. A values that's passed in as a parameter to a function, for example, will be left alone until the function exits. That does not stop you from deriving other values from the variable or using it to make a decision - if you couldn't, there would be no point in passing it in to the function!
Here are two C++ methods taking a float into a function. In teh first case I have commented out the calculation that would change the incoming value as it lead to a compile time error.
void Hotel::setoccu(const float *inval) {
// inval *= 0.9; // Compiler complain like sh**
occrate = *inval * 0.9;
}
void Hotel::setoccu(float inval) {
inval *= 0.9; // Compiler won't complain like sh**
occrate = inval;
}
Why use const ?
1. Efficiency of code / may run a bit quicker
2. Safety / provides an extra check to help the programmer
Some other articles
C234 - Further C++ Object Oriented features When do I use the this keyword in C++? Designing a base class and subclasses, and their extension, in C++ Changing what operators do on objects - a comparison across different programming languages Using a vector within an object - C++ Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses Sigils - the characters on the start of variable names in Perl, Ruby and Fortran Bradshaw, Ben and Bill. And some C and C++ pointers and references too. C++ - putting the language elements together into a program Strings, Garbage Collection and Variable Scope in C++ Lots of things to do with and within a C++ class What are C++ references? Why use them? The Multiple Inheritance Conundrum, interfaces and mixins Multiple Inheritance in C++ - a complete example This article Variables and pointers and references - C and C++ Variable Scope in C++ Calling base class constructors It can take more that one plus one to get two. Comparison of Object Oriented Philosophy - Python, Java, C++, Perl undefined reference to typeinfo - C++ error message Simple polymorphism example - C++ C201 - C Language Fundamentals C - why is slow to write and debug) but fast to run? Preprocessor directives in C and C++ - what they mean Learning to program - variables and constants BODMAS - the order a computer evaluates arithmetic expressions Integer types, and integer overflows, in C Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. Your program - you just provide the filling in the sandwich Learning to write good programs in C and C++ - separating out repeated code Staring a C course with Hello World - why? This article Variables and pointers and references - C and C++ Compiling C programs with gcc - an overview Turning C from source to a running program