C++
  Home arrow C++ arrow 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  
Download TestComplete 
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 / 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

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
    (Page 1 of 6 )

    You can do more with the const keyword in C++ than you can in C. Many programmers avoid using it, however, in part because it can be a little tricky to understand at first. Jun Nakamura explains how to use the keyword, and the excellent effect it can have on your code.

    Introduction

    The const keyword has become quite versatile when you compare its abilities in C++ to its abilities in C. Unfortunately I have to say that I see more programmers avoiding it than using it to their benefit. Most often the reason they refuse to use it is simply because they feel it just gets in their way or because they find it impossible to introduce it into a project that has been going for a while. After all const-correctness is something you should implement from the beginning and not introduce as an afterthought.

    Still if you spend a little bit of time looking into the effect this keyword has on your code, it can become your greatest friend. This friend may be a bit hard to understand at first though. Just take a look at the following declarations:

    constchar *str  = “this looks very familiar”;

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

    char * const str = “what is const here?”;

    constchar * const str = “now it is getting stranger”;

    char const * const str = “and stranger!”;

    This awkward placing of const confuses more than it helps, but once you know how to interpret the effect of const correctly, you will dictate which variable/objects should not be modified! Not only are you communicating this to the compiler so that it can help you track possible violations of that constraint, but you are also communicating this to other programmers (including yourself) that try to make good use of your code.

    More C++ Articles
    More By J. Nakamura


     

    C++ ARTICLES

    - 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
    - Temporary Variables: Temporaries Are Not Nec...
    - Temporary Variables: Keep Your Values Close,...
    - Temporary Variables: Procrastination is the ...
    - Who`s Afraid to Be Const Correct? Help Your ...
    - Persistent Data: File Input and Output


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





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