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  
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 / 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++


    (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

    - 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
    - Iostream Library and Basic I/O in C++
    - Introduction to Streams







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