C++
  Home arrow C++ arrow Page 5 - Who`s Afraid to Be Const Correct? Reading ...
IBM developerWorks
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  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    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

    - Large Numbers
    - Dijkstra`s Shunting Algorithm with STL and C...
    - Brief Introduction to the STL Containers
    - The Standard Template Library
    - Templates in C++
    - 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







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