C++
  Home arrow C++ arrow 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  
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? 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

    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? Take Your Object`s Bits Literally


    (Page 1 of 5 )

    This is the second article in a tutorial series covering const correctness. In this article, Jun Nakamura discusses some problems concerned with bitwise copy construction and bitwise constness, how to make your interface conceptually const, and how to make changes to an object that are not visible to the user.

    A Bitwise Problem

    Using const to strengthen the contracts in your code (like applying it to member functions to guarantee that the function won’t change the internal state of an object) is a good thing. But C++ would not be C++ if things couldn’t be made confusing one way or the other. First consider the following class:

    class MyClass {

    public:

     // ctor makes m_pLabel point to a copy of label MyClass(char const *label) {

      m_pLabel=new char[MAX_LABEL_LEN];

      SetLabel(label);

      }

     

    ~MyClass() {

     try { delete m_pLabel; }

     catch (...) { }

     }

     

     char* GetLabel() const { return m_pLabel; }

     

     void SetLabel(char const *label) {

      assert(label);

    assert(static_cast<int>(strlen(label)+1)

     < MAX_LABEL_LEN);

    (void)sprintf(m_pLabel, “%s”, label);

    }

    private:

     static int const MAX_LABEL_LEN;

     char *m_pLabel;

    };

    int const MyClass::MAX_LABEL_LEN = 1024;

     

    This is not a piece of code I would recommend you ever use (or write for that matter!), but it will serve my purpose here. Did you spot at least one big problem this class has?

    The problems I am looking for are concerned with bitwise copy construction and bitwise constness.

    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 3 hosted by Hostway