C++
  Home arrow C++ arrow Page 2 - Who`s Afraid to Be Const Correct? Reading ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Moblin 
JMSL Numerical Library 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C++

Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 9
    2005-08-16

    Table of Contents:
  • Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
  • Const Declarations
  • The Constant Value vs. La Valeur Constante
  • Syntactical Substitution Problems
  • Const Member Functions
  • Right There Right Now

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Who`s Afraid to Be Const Correct? Reading Const Correctly in C++ - Const Declarations


    (Page 2 of 6 )

    The const keyword can be used inside classes for static/non static data members and function members (and their respective arguments). Outside classes it can be used for global or namespace constants, function arguments and for static variables (these are variables that are for the private use of the functions in their respective source files, and are not meant to be accessed by anything else [K&R]).  For pointers and references you can specify whether the pointer or reference itself is const, the data pointed to or referenced is const… or both.

    Constants may also help you to get rid of ‘magic values’ in your code. This means that instead of ‘char msg[128];’ you can write more readable declarations like ‘char msg[MAX_MSG_LEN];’. The first declaration uses a ‘magic value’ because it is not immediately clear what 128 stands for (it just magically appears). On the other hand ‘MAX_MSG_LEN’ can be read and understood to represent the maximum message length.

    An additional benefit of using constants is that you can group them together at the beginning of your source code and reuse them whenever possible (‘MAX_MSG_LEN’ can easily be used to declare an array more than once in your code!).

    Please don’t declare MAX_MSG_LEN this way:

    #define MAX_MSG_LEN 128

    It works (the preprocessor replaces MAX_MSG_LEN with 128 before your code is seen by the compiler) but #defines are very hard (and clumsy) to debug… and they are not typesafe!

    Instead use a declaration like this:

    int const MAX_MSG_LEN = 128;

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - Multiplying Large Numbers with Karatsuba`s A...
    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - C++ Programmer Alerts
    - C++ Programming Tips
    - First Steps in (C) Programming, conclusion
    - First Steps in (C) Programming, continued
    - First Steps in (C) Programming, introduction
    - C++ Preprocessor: Always Assert Your Code Is...
    - C++ Preprocessor: The Code in the Middle
    - Programming in C
    - Temporary Variables: Runtime rvalue Detection






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT