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  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
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 / 10
    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++ - 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

    - More Tricks to Gain Speed in Programming Con...
    - Easy and Efficient Programming for Contests
    - Preparing For Programming Contests
    - Programming Contests: Why Bother?
    - Polymorphism in C++
    - Overview of Virtual Functions
    - Inheritance in C++
    - Extending the Basic Streams in C++
    - Using Stringstreams in C++
    - Custom Stream Manipulation in C++
    - General Stream Manipulation in C++
    - Serialize Your Class into Streams in C++
    - Advanced File Handling with Streams in C++
    - File Handling and Streams in C++
    - The STL String Class







    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek