Home arrow C++ arrow Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally
C++

Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally


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.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 7
August 23, 2005
TABLE OF CONTENTS:
  1. · Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally
  2. · Bitwise Copy Construction
  3. · Bitwise Constness
  4. · Make Your Interface Conceptually Const.
  5. · Const and Mutable

print this article
SEARCH DEVARTICLES

Who`s Afraid to Be Const Correct? Take Your Object`s Bits Literally
(Page 1 of 5 )

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.


blog comments powered by Disqus
C++ ARTICLES

- Intel Threading Building Blocks
- Threading Building Blocks with C++
- Video Memory Programming in Text Mode
- 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++

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials