- const const Everywhere a CONST -

In all my years of C++ programming, I never used a const.  However, we are trying to do things the right way in this course.  You can program without consts; but, when you are writing code that is going to be maintained by others and/or shared with others using, using consts is the right thing to do. 

You may also find that six months after you write some code, it is as if you are looking at someone else's work - so you may be doing yourself a favor.

In this discussion I am going to try and cover all the consts we are using in this course.

1) The const method

You can make a method constant.  You can not make a function constant.  Constantness (I made that word up) in a method implies that the method will not modify instance data and this makes no sense with respect to a function.  For example:

class a{
   int x;
   void f(int z) const {
      //x = 3; // will not compile
      z = 3; // fine
   }
};

2) The constant pointer/ pointer constant

You can make a constant pointer.  

#include <iostream.h>
void main(){
   float const *pf;
   float x = 3.7;
   float y = 4.2;
   pf = &x;
   pf = &y;
   cout << *pf << endl;
}

Compile the above code! What? This code compiles fine!  What is going on here?  We declared the pointer constant, yet we can assign it any value?  How can we cause a compiler error?


   *pf = 7.7; // gives an error
   

We see then, that the declaration  

    float const *pf;  

creates a pointer that can be dereferenced but that can not be used to assign a value to a memory location.  It is a kind of read only pointer. (so thats what a constant pointer is...)

What if we change the placement of the const keyword in the declaration of pf? As in:

#include <iostream.h>
void main(){
   float x = 3.7;
   float y = 4.2;
   float * const pf; // just swap const *
   pf = &x;
   pf = &y;
   cout << *pf << endl;
   *pf = 7.7;
}

Compile the code and: Blimey, that creates a plethora of errors.  Why? Well, when we place the keyword const next to the variable pf, we change the constantness so that pf is a constant variable that is also a normal pointer.  We can correct the code by entering:

#include <iostream.h>
void main(){
   float x = 3.7;
   float y = 4.2;
   float * const pf = &x; // ok now
   //pf = &y; // will not work
   cout << *pf << endl;
   *pf = 7.7;
}

But wait!  Curiouser and curiouser.  Now our variable pf, which is constant, can be used to modify x!

Lets summarize the results:

A constant pointer A pointer that is a constant

const  *p

* const

can point to any variable of its type but can not change the value of the variable.  a read only pointer. can only point to one variable, but can change that variable's value.

So can we create a constant pointer that is constant?  As Curly would say to Moe: Nyuk, Nyuk, Nyuk - sointly.

#include <iostream.h>
void main(){
    float x = 3.7;
    float y = 4.2;
    float const * const pf = &x; 
    //pf = &y; // will not work
    cout << *pf << endl;
    //*pf = 7.7; // will not work.
}

What have we created then?  Why a constant (read only) pointer that can only point to one variable!

3) The constant pointer/ pointer constant/ or constant pointer constant in a constant method with a reference. (cheer up we are almost done)

Naturally we are not done yet.  What does a method prototype like

 f(int const * const &ptr)const imply?

#include <iostream.h>
class a{
public: 

   int m;
   void f(int const * const &ptr) const{
   cout << *ptr << endl;
   }
};
void main()
{
   int i = 7;
   int *pi = &i;
   a x;
   x.f(pi);
}

Compile the program and run it. It works. So lets see what we have here.  Since the variable ptr is preceeded by & we have a reference variable.  ptr is not a copy of pi but is actually pi.  So what limitations did the prototype put on us? Well, in addition to the reference, we had 3 consts

f(int const(1) * const(2) &ptr)const(3)

const (1) implies that this is a read only pointer - we can not change the value of pi by doing:

*ptr = 42; // not allowed

const (2) implies that this is a constant pointer variable - we can not change what variable ptr is pointing to such as:

ptr = &m; // not allowed

const (3) implies that this is a constant method - we can not change any instance variables in this method such as:

m = 17; // not allowed

 

 

______________________________________________

That is all I wanted to cover in this discussion.  If you have anything else you would like me to explain, email me.