Const Confusion

This is a discussion on Const Confusion within the C++ Programming forums, part of the General Programming Boards category; 'const' is the keyword used when you don't want the variable to change throughout the program. But why would you ...

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    38

    Question Const Confusion

    'const' is the keyword used when you don't want the variable to change throughout the program. But why would you need 'const' if there is no risk that the variable can change. I've seen const in some programs that i think really doens't need to be there. Is it just a visual aid to the reader, saying that "This variable is one that doesn't change."??
    #include <Jesus.h>
    It will save your life

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    no not at all. const behind the scenes lets your compiler make all sorts of optimisations that would otherwise be near impossible. There is a good article on const correctness here. Read the others too.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Say you are making a series of functions that anyone can use to do matrix maths. If you have a constant defined in your header file, and the user has a variable by the same name, renames it so your functions will work, but forgets to rename an instance where a value is assigned to it, an error will show. Otherwise, the value would be changed, most likely screwing up all of your functions.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Registered User
    Join Date
    Mar 2003
    Posts
    38
    woh! oops, well, thanks for correcting me.
    #include <Jesus.h>
    It will save your life

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 08:32 AM
  2. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  3. Drawing Program
    By Max_Payne in forum C++ Programming
    Replies: 21
    Last Post: 12-21-2007, 04:34 PM
  4. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21