Home arrow C++ arrow Page 4 - Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
C++

Who`s Afraid to Be Const Correct? Reading Const Correctly in C++


You can do more with the const keyword in C++ than you can in C. Many programmers avoid using it, however, in part because it can be a little tricky to understand at first. Jun Nakamura explains how to use the keyword, and the excellent effect it can have on your code.

Author Info:
By: J. Nakamura
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
August 16, 2005
TABLE OF CONTENTS:
  1. · Who`s Afraid to Be Const Correct? Reading Const Correctly in C++
  2. · Const Declarations
  3. · The Constant Value vs. La Valeur Constante
  4. · Syntactical Substitution Problems
  5. · Const Member Functions
  6. · Right There Right Now

print this article
SEARCH DEVARTICLES

Who`s Afraid to Be Const Correct? Reading Const Correctly in C++ - Syntactical Substitution Problems
(Page 4 of 6 )

Another reason to put const after that which you would like to apply it to, is that things become very muddy when you apply const to syntactical substitutions. Assume the following typedef:

typedef int* INTPTR;

You saw that ‘const int’ and ‘int const’ have the same meaning: the int value is constant. But how does the compiler interpret ‘const CHARPTR’ or ‘CHARPTR const’? Have a look at the following test example:

// test.cpp

#include <stdio.h>

typedef int* INTPTR;

void foo1(const INTPTR val) { *val = 42; }

void foo2(/*const*/ int* val) { *val = 42; }

void bar1(INTPTR const val) { *val = 42; }

void bar2(int * const val)  { *val = 42; }

int main()

{

 int value = 0;

 foo1(&value);

 (void)printf(“value is %d\n”, value);

  value = 0;

  foo2(&value);

  (void)printf(“value is %d\n”, value);

  value = 0;

  bar1(&value);

  (void)printf(“value is %d\n”, value);

  value = 0;

  bar2(&value);

  (void)printf(“value is %d\n”, value);

 return 0;

}

Were you surprised that the compiler didn’t complain and that ‘value’ was assigned ‘42’ every time? If you expected ‘const INTPTR’ to be interpreted as ‘const int *’, you were definitely in for a surprise, because somehow you were allowed to change the data pointed at to 42 when value was declared as ‘const INTPTR’ but not when it was ‘const int *’!  

Apparently ‘const INTPTR’ does not translate to ‘const int *’… what is going on?

Lets consider ‘const int i;’ for a moment again. We saw that there is no difference between ‘const int i;’ and ‘int const i;’. Both statements declare ‘i’ as being constant. The same thing happens with the typedef. The compiler interprets ‘const INTPTR i;’ as if it was declared ‘INTPTR const i;’ and the last statement expands to ‘int * const i;’ declaring the pointer constant -- and not the data pointed at!


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