Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
What does const mean? C and C++

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
(written 2010-01-15)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
C201 - C and C based languages - C Language Fundamentals
  [4324] Learning to program - variables and constants - (2014-11-22)
  [3917] BODMAS - the order a computer evaluates arithmetic expressions - (2012-11-09)
  [3591] Integer types, and integer overflows, in C - (2012-01-25)
  [3278] Do I need to initialise variables - programming in C, C++, Perl, PHP, Python, Ruby or Java. - (2011-05-05)
  [3234] Your program - you just provide the filling in the sandwich - (2011-04-08)
  [3120] Learning to write good programs in C and C++ - separating out repeated code - (2011-01-04)
  [2842] Staring a C course with Hello World - why? - (2010-06-30)
  [2005] Variables and pointers and references - C and C++ - (2009-01-23)
  [1671] Compiling C programs with gcc - an overview - (2008-06-10)
  [888] Turning C from source to a running program - (2006-10-06)

C234 - C and C based languages - Further C++ Object Oriented features
  [3982] Using a vector within an object - C++ - (2013-01-19)
  [3509] Operator Overloading, Exceptions, Pointers, References and Templates in C++ - new examples from our courses - (2011-11-06)
  [3430] Sigils - the characters on the start of variable names in Perl, Ruby and Fortran - (2011-09-10)
  [3238] Bradshaw, Ben and Bill. And some C and C++ pointers and references too. - (2011-04-09)
  [3124] C++ - putting the language elements together into a program - (2011-01-08)
  [3069] Strings, Garbage Collection and Variable Scope in C++ - (2010-11-25)
  [3057] Lots of things to do with and within a C++ class - (2010-11-16)
  [2849] What are C++ references? Why use them? - (2010-07-02)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2673] Multiple Inheritance in C++ - a complete example - (2010-03-12)
  [2004] Variable Scope in C++ - (2009-01-22)
  [1819] Calling base class constructors - (2008-10-03)
  [1159] It can take more that one plus one to get two. - (2007-04-22)
  [831] Comparison of Object Oriented Philosophy - Python, Java, C++, Perl - (2006-08-13)
  [802] undefined reference to typeinfo - C++ error message - (2006-07-15)
  [801] Simple polymorphism example - C++ - (2006-07-14)


Back to
Sharing variables between files of code in C - extern
Previous and next
or
Horse's mouth home
Forward to
Complete teaching example - C++, inheritance, polymorphism
Some other Articles
C course inspires new teaching examples
Creating, setting up and using objects in C++
Where are your objects stored in C++?
Complete teaching example - C++, inheritance, polymorphism
What does const mean? C and C++
Sharing variables between files of code in C - extern
Summary of Wiltshire Core Strategy responses
C Structs - what, how and why
The what and why of C pointers
Reading and writing files in C
4320 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2576_Wha ... nd-C-.html • PAGE BUILT: Thu Sep 18 15:30:25 2014 • BUILD SYSTEM: WomanWithCat