C++
  Home arrow C++ arrow Page 5 - 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++ - Const Member Functions


    (Page 5 of 6 )

    Another use of const not available in C, but very useful in C++, is the possibility to declare member functions const. This means that you are promising that the member function won’t change the value of any of the data members of the object.

    Imagine what would happen when the function membersize() of some class List would change the order of elements in that list while you were iterating through it! By declaring the memberconst it is guaranteed that this cannot happen, and it will force any other programmer that will be maintaining that code not to touch the member values either.

    The declaration of a constant member functionsize() looks like this:

    size_t size() const; 

    You will find them most often used when a class doesn’t expose its data members in the public interface [Meyers]. Such a class will provide ‘get’ and ‘set’ functions in its public interface, and since it makes sense for something that reads out a value not to change that value (or any other values in the object that is being accessed), you will notice ‘get’ functions declared as constant member functions.

    A const member function guarantees that an object’s data members will remain untouched, so these functions can only be invoked on a const object. Member functions can behave differently depending on whether they are declared const or not. They can be overloaded exactly for this purpose. That is why you will find two implementations of ‘operator[ ]’ in the STL std::vector class:

    // returns a const reference to an element you can only read

    const_reference operator[](size_type _Pos) const

    { // subscript nonmutable sequence

    return (*(begin()+_Pos));

    }

    // returns a reference to an element you can manipulate

    reference  operator[](size_type _Pos)

    { // subscript mutable sequence

     return (*(begin()+_Pos));

    }

    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-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT