CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Oct 2009
    Posts
    29

    Question regarding "const"

    Ok this may be a stupid questions however, the book I am studying from says that following the principles of least privilege you should always declare a variable (arrays and such) const if the function is not going to manipulate the data. IE: just printing out and array or something similar. My question is what difference does it make weather I do or not. If I don't code the function to change the value then why is const so important and stressed so much as good eng practice. Thanks in advance for your replies.

  2. #2
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Lightbulb Re: Question regarding "const"

    Because the compiler will report an error if your code accidentally manipulates data that it shouldn't. Using the const qualifier makes it very difficult to accidentally modify data or pass the const data to functions that take non-const pointers or references. By using the const qualifier you are helping the compiler which in turn will help you find errors before you run!

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Question regarding "const"

    Quote Originally Posted by robwong81
    If I don't code the function to change the value then why is const so important and stressed so much as good eng practice.
    What if you, or someone who goes on to maintain your code, changes the function by accident such that it does change that value, but elsewhere you rely on the assumption that it does not?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question regarding "const"

    Let's say you're working with another engineer. He's being const-correct, and you aren't. Then his function (which takes a const array as a parameter) calls your function to print it out---but he gets a compile error, because he can't pass his const array to your function expecting a non-const array.

    That is the basic problem: As soon as you start using const a little bit, you have to use it everywhere, or you run into trouble. You can't do things in half measures.

    So why use it at all? Well, why not. Using const has several advantages:
    1) It's a form of self-documentation, letting users of the code (who aren't you) know something about the semantics.
    2) It's an easy way to increase compile-time checking. You always want your errors to be caught at compile time rather than runtime if at all possible, and const is a trivial means of helping out with that.
    3) In a few very specific cases, it can help the compiler do a better job of optimizing your code. Since the programmer is able to bypass const-ness, this isn't as big a factor as it could be, however.

  5. #5
    Join Date
    Oct 2009
    Posts
    29

    Re: Question regarding "const"

    Ok so then it seems it has more of a structured usage than an actual needed use. Its main purpose is for maintaining code and working with others who use it in a big project that your a part of. Thanks to all of you for your quick and discerning answers.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Question regarding "const"

    Yeah, it's there to make your life easier, but the code won't run any differently without it.

  7. #7
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Question regarding "const"

    Quote Originally Posted by robwong81 View Post
    Ok so then it seems it has more of a structured usage than an actual needed use. Its main purpose is for maintaining code and working with others who use it in a big project that your a part of. Thanks to all of you for your quick and discerning answers.
    Not 100% true. If you are coding on an embedded environment, then your const objects may not be in the same address space as your non-const objects. Also, using const may help your compiler create create more efficient code:

    Code:
    const int a = 5;
    doSomething(a);
    print(a);
    Look at the above code. Your compiler will be smart enough to know that a will be 5 throughout the program, and won't bother "checking if it really is". If a was not const, your compiler would have to check a after doSomething, just in case doSomething changed a.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured