C++
  Home arrow C++ arrow Page 4 - 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  
Actuate Whitepapers 
Moblin 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Who`s Afraid to Be Const Correct? Reading Const Correctly in C++ - Syntactical Substitution Problems


    (Page 4 of 6 )

    Another reason to put const after that which you would like to apply it to, is that things become very muddy when you apply const to syntactical substitutions. Assume the following typedef:

    typedef int* INTPTR;

    You saw that ‘const int’ and ‘int const’ have the same meaning: the int value is constant. But how does the compiler interpret ‘const CHARPTR’ or ‘CHARPTR const’? Have a look at the following test example:

    // test.cpp

    #include <stdio.h>

    typedef int* INTPTR;

    void foo1(const INTPTR val) { *val = 42; }

    void foo2(/*const*/ int* val) { *val = 42; }

    void bar1(INTPTR const val) { *val = 42; }

    void bar2(int * const val)  { *val = 42; }

    int main()

    {

     int value = 0;

     foo1(&value);

     (void)printf(“value is %d\n”, value);

      value = 0;

      foo2(&value);

      (void)printf(“value is %d\n”, value);

      value = 0;

      bar1(&value);

      (void)printf(“value is %d\n”, value);

      value = 0;

      bar2(&value);

      (void)printf(“value is %d\n”, value);

     return 0;

    }

    Were you surprised that the compiler didn’t complain and that ‘value’ was assigned ‘42’ every time? If you expected ‘const INTPTR’ to be interpreted as ‘const int *’, you were definitely in for a surprise, because somehow you were allowed to change the data pointed at to 42 when value was declared as ‘const INTPTR’ but not when it was ‘const int *’!  

    Apparently ‘const INTPTR’ does not translate to ‘const int *’… what is going on?

    Lets consider ‘const int i;’ for a moment again. We saw that there is no difference between ‘const int i;’ and ‘int const i;’. Both statements declare ‘i’ as being constant. The same thing happens with the typedef. The compiler interprets ‘const INTPTR i;’ as if it was declared ‘INTPTR const i;’ and the last statement expands to ‘int * const i;’ declaring the pointer constant -- and not the data pointed at!

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - 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
    - Temporary Variables: Chasing Temporaries Away







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway