C++
  Home arrow C++ arrow Page 3 - 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  
Dedicated Servers  
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++ - The Constant Value vs. La Valeur Constante


    (Page 3 of 6 )

    First I want to get rid of the confusion a const declaration can create. This confusion simply arises because of the two different types of programming styles the C++ standard allows: const int PI=3.14159265 has the same meaning as int const PI=3.14159265. Although it is much less common, I do prefer to use int const rather than const int to specify a “constant integer.” (Note that the French put the adjective after the noun instead of the other way around; so I can imagine that they wouldn’t find it strange to use ‘int const’ at all!)

    Let me explain why I prefer it this way…

    Personally, I prefer a consistent way to answer the question “What is constant?” Apparently there are two ways to answer this question (‘int const’ and ‘const int’): “either what comes after or before the const qualifier is const”… ouch! Not very helpful! 

    But with pointers (and references) it becomes a bit more complicated. When we apply const to a pointer there are two things we can constrain: the data pointed at (the user can’t make any modification to the data using the pointer) and/or the pointer itself (the user can’t change the address inside the pointer variable). 

    // a non-const pointer with non-const data

    char *str = “you can modify this text”;

    // a non-const pointer with const data

    const char *str = “this looks very familiar”;

    // another non-const pointer with const data

    char const *str = “though this has my preference”;

    // a const pointer with non-const data

    char * const str = “you cannot modify the pointer”;

    // a const pointer with const data

    const char * const str = “cannot change anything”;

    // another const pointer with const data

    char const * const str = “and neither can this be changed”;

    You see that it is impossible to apply the rule “const refers to what follows it” when we apply const to a pointer or reference. Take for example ‘char * value’. If we apply the rule when we try to make the pointer constant, it would become ‘char const * value’. Unfortunately this doesn’t make the pointer constant, but the data pointed at! Yes I agree: this is very confusing.

    However the rule “const refers to what is in front of it” works when we apply it to a pointer or reference. The same example ‘char * value’ becomes ‘char * const value’. If we would like to make both the pointer and the data pointed at constant we would declare ‘char const * const value’.

    Consistency pays off and I stick to this rule. Do you want to know which element is const? Just look at what is preceding the const keyword.

    When you are reading other people’s code you should of course still understand what ‘const char * const’ implies.

    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 1 hosted by Hostway