C++
  Home arrow C++ arrow Page 4 - Who`s Afraid to Be Const Correct? Take You...
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? Take Your Object`s Bits Literally
By: J. Nakamura
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2005-08-23

    Table of Contents:
  • Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally
  • Bitwise Copy Construction
  • Bitwise Constness
  • Make Your Interface Conceptually Const.
  • Const and Mutable

  • 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? Take Your Object`s Bits Literally - Make Your Interface Conceptually Const.


    (Page 4 of 5 )

    We are going to extend the class and provide a function that transforms the label into uppercase.

    void LabelToUpper() ;

    The question I have now is whether or not this should be a constant member function. Since the function won’t make any bitwise changes to our class (the address in pointer m_pLabel remains untouched), we can guarantee this to the user by making the function const, right?

    Not really. This is where you have to stick to the concept of const and think about how the class will be perceived by the user. Even though none of the data members are changed in a bitwise manner, the contents of the class have changed in a conceptual way. Therefore don’t declare the function as const, because it will only confuse matters!

    Aliasing Problem

    Take a look at the following function:

    void foo(int const &a, int &b) {

     int temp=a;

     ++b;

     assert(a==temp);

    }

    Is it possible to trigger the assert? Surely because ‘a’ is declared const, it will always equal ‘temp’?

    Actually, there is an exception:

    int value = 42;

    foo(value, value);

    Now both ‘a’ and ‘b’ reference the same variable, and it is perfectly legal to make changes to this variable through the ‘b’ reference. This is also known as the aliasing problem, and it applies in the same manner to pointers.

    Although the problem is very clear when stated this way, things can get pretty obfuscated when the function with the aliasing problem is nested deep inside the call stack and defined in another source file. It may even be part of a library for which you don’t have the source code!

    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 2 Hosted by Hostway
    Stay green...Green IT