C++
  Home arrow C++ arrow Page 2 - Const Correctness in C++
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++

Const Correctness in C++
By: Gabor Bernat
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 3
    2008-10-28

    Table of Contents:
  • Const Correctness in C++
  • Constant Variables
  • Const in Functions and in Classes
  • Conclusion

  • 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


    Const Correctness in C++ - Constant Variables


    (Page 2 of 4 )

    Constant, according to the Free Online Dictionary, means: Unchanging in nature, value, or extent; invariable. This pretty much tells you all you need to know about them in the beginning. Basically, C++ inherited this from the C language. There already, a constant variable was nothing more than one whose value remained constant throughout the execution of the program once a value had been assigned to it upon initialization.

    Because writing a program can be time-consuming, and programmers are naturally lazy and prefer to accomplish much without writing much, in C/C++ the constant keyword has been reduced to const. As C/C++ includes more than just plain variables, the const keyword has been introduced for pointers and references as well. Below I will show you an example of each one and explain them afterward. 

    1.    int I = 1;

    2.    const int I = 2;

    3.    const int* I = 3;

    4.    int const * I = 3;

    5.    int* const I = 2;

    6.    const int* const I = 1;

    7.    const int& I = 2;

    8.    int& const I = 2;

    9.    typedef int_co_ref int&;

    10.int_co_ref const I = 2;

    Now there we have all of the combinations possible, but what does each of them mean? Well, the first one should be obvious if you are still reading this text. As for the second one the situation should be same. All we did was declare a variable that is constant-unchangeable and initialize it with the value 1. From the point of declaring the variable through any point where the variable's declaration is still valid (aka in the interior of the scoop (parenthesis - {} you declared), we can use it instead of writing the number 1.

    The third example represents a pointer pointing to a const variable. Once it is created, you can't modify the variable to which it points, but you are free to assign to the pointer a different variable. Here we can also include the fourth as it is the same; the compiler lets us write the const keyword where we wish.

    Now the fifth example is more intriguing. We create a pointer that is a const. That means that it won't be able to change the variable through the pointer. However, through a different pointer or through the variable itself we are free to make any changes. Simply put, we have a pointer that, once assigned, cannot be changed, but that applies only to the pointer and not to the variables related to it.

    The last one is the const from all points of view. Put the second and fifth examples together and you will get one that looks just like the sixth. A const (unchangeable) pointer points to a constant variable. This is an excellent method to use if you want to be sure that the input data won't change in a function.

    The const reference is something that isn't. Okay now I know that sounds awkward, but the truth is that you can't create one like this. Simply put, it isn't correct grammatically. And to underline my statement, the eighth row in the upper code snippet will return a syntax error. Of course you can create one with rows 9 and 10, but the compiler would ignore it. The const here is redundant and will be ignored by the compiler.

    The the upper statements are all true because a reference in its essence is a binder to a variable. Once you bind it to one, you cannot bind it to another; it can't refer to another variable. The reference is in its nature const.

    More C++ Articles
    More By Gabor Bernat


     

    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 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek