Home arrow C++ arrow Page 2 - 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 - Bitwise Copy Construction
(Page 2 of 5 )

The bitwise copy construction problem falls outside the const topic, but since it is conceptually related to bitwise constness, I don’t want to keep it from you. Whenever you write or see a class that has a pointer as a data member, I want you to think of these (possible) bitwise problems and check that they have been solved for that particular class.

Take a look at the following test code:

MyClass *label1=new MyClass(“value1”);

(void)printf(“Label1: %sn”, label1->GetLabel());

MyClass *label2=new MyClass(“value2”);

(void)printf(“Label2: %sn”, label2->GetLabel());

label2=label1;

(void)printf(“Label1: %sn”, label1->GetLabel());

(void)printf(“Label2: %sn”, label2->GetLabel());

When you compile and run this code you will get the output you are expecting:

Label1: value1

Label2: value2

Label1: value1

Label2: value1

Did you think twice, however, when you saw the statement ‘label2=label1;’? Who created the assignment operator? (And yes, there is also a copy constructor available –- where did that come from?)

The C++ compiler will supply you with a copy constructor and an assignment operator, unless you specify them yourself. The copy construction and assignment are done in a bitwise manner, meaning that they will copy the value of the data members from the original class to the data members of the other class bit by bit.

For MyClass this means that *m_pLabel will be copied bit by bit from ‘label1’ to ‘label2’: so afterwards label1.m_pLabel and label2.m_pLabel are both pointing to the same memory location!

To prove my point:

label2->SetLabel(“value2”);

(void)printf(“Label1: %sn”, label1->GetLabel());

(void)printf(“Label2: %sn”, label2->GetLabel());

This will provide us with the following output:

Label1: value2

Label2: value2

The most obvious solution in this case would be to implement our own copy constructor and assignment operator. In one of my previous articles about pointers, I demonstrated how you can prevent your class from being copyable by declaring the copy constructor and assignment operator private.

The interface for MyClass would look like this:

class MyClass {

public:

  MyClass(char const *label);

 ~MyClass();

 

 char* GetLabel() const;

void  SetLabel(char const *label);

 

   private:

  MyClass(MyClass const&);

  MyClass& operator=(MyClass const&);

private:

 static int const MAX_LABEL_LEN;

 char *m_pLabel;

};


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