Technical Note

: SLE0009


: Scott Evans


: 22/9/99


: The const keyword 

The const keyword is probably one of the most confusing aspects of the C/C++ language. It can have several meanings depending on where it is used. I wrote this technical note so the next time I get confused I can refer to it and it will all be crystal clear.

The most common use for the const keyword is declaring constant values. In C #define is used mostly instead of const. The advantage of const in C++ is it allows for the compiler to use type checking. Once the constant variable is initialised with a value it cannot be changed. In C++ constant variables can be used for declaring the size of an array.

The code below creates an array of 100 integers. It uses a const variable MAX_SIZE to define the size of the array.

const int MAX_SIZE=100;

int array[MAX_SIZE];

The compiler would not let you do the following. It would cause a compiler error.


We can also tag the const keyword onto C++ member functions. If a member function does not change any member variables then the function can be declared as const. This helps the compiler with optimisation issues and will produce compiler errors where appropriate.

The code below shows how you could implement a rectangle class. You could make the dimensions of the rectangle private in which case you would need set() and get() member functions. Since the get() functions are not supposed to change the member variables they can use the const keyword. If by mistake your get() member functions change the member variables the compiler will have a fit and not compile the code.

class cRECT



int x,y;

int w,h;


void set(int x,int y,int w,int h);

int getx(void) const {return(x)};

int gety(void) const {return(y)};

int getw(void) const {return(w)};

int geth(void) const {return(h)};


So the member function below will not compile.

int getx(void) const





The only thing to note here is if a member function returns a reference or pointer to a member variable the compiler will complain if the function is const.

You can also use const to declare a const pointer, a pointer to const data or a const pointer to const data. Let us start with a constant pointer.

int i;

int *const ptr=&i;


What this means is once the pointer has been set it cannot ever be changed. You can change the data that the pointer points to but if you try to reassign the pointer the compiler will produce an error.

So this code will cause a compiler error.

int j;


A pointer (or reference) to constant data on the other hand lets you change the pointer (or reference) but not the data.

const int i=10;

const int *ptr;


The following code in this case will cause a compiler error.


The last thing is a constant pointer to constant data. Once declared neither the data nor the pointer can be changed.

const int i=30;

const int *const ptr=&i;

So this code will not compile.


const int j=80;


All this can be used to help you the programmer by giving the compiler more opportunity to catch errors at compile time. Lets us take the C++ copy constructor and the C strcpy() function as examples of the const keyword being used.

All C++ classes have a copy constructor. If you do not define one the compiler will produce a default copy constructor. Now the copy constructor is used to construct an object by copying another object of the same type. You would not want the copy constructor to modify the original object. So the const keyword can be used to let the compiler know this.

Let us look at the copy constructor for the cRECT class assuming we made the member variables public.

cRECT(const cRECT &r)







Now this will compile since we are not changing the source object r but the following version would not compile.

cRECT(const cRECT &r)









The C function strcpy() has the following function prototype.

char *strcpy(char *,const char *);

This tells the compiler that the function strcpy() cannot change the string that is being copied as the function is supposed to copy a string and not modify the source string.

This document in word format.
This document in text format.