saxon switzerland, rock, rock needle-539418.jpg

Const in C++

Const in C++

Decalare a variable
x and assign the value 42 to it.
int x = 42;

We now have a mutable type
x that can have any integer value. However, if we add the const keyword
const int x = 42;
x is now an immutable variable and cannot be modified. You can use this for instance for max and min boundary values in your code:
const int MAX = 42;
const int MIN = 0;

Now we create a pointer x to an integer
int* x = new int;
You can dereference the x pointer and assign a value
*x = 21;
Print the contents of the variable:
std::cout << *x  << std::endl
Or you can assign MAX to x by reassigning the pointer to MAX
x = (int*)&MAX;
See function Test1() in the following code fragment:

				
					void Test1() {
    const int MAX = 42;
    int* x = new int;
	*x = 21;
	std::cout << *x << std::endl;
	x = (int*)&MAX;
	std::cout << *x << std::endl;
}

				

Now we will add the const keyword to the pointer
const int* x = new int
Dereferencing x and assign as new value is not possible. The following code will generate an error:
*a = 21;
What we can do is assign a new address to the pointer. Point x to the MAX constant
x = (int *)&MAX;
You cannot change the contents of the pointer but you can change the address the pointer points to (see the next code example)

				
					void Test2() {
	const int MAX = 42;
	const int* x = new int;
	// you could also use
	// int const* x = new int;
	// dereferencing x and assign a new value is not possible
	// *x = 21; // ERROR
	x = (int*)&MAX;
	std::cout << *x << std::endl;
}
				

We can also do this in another way:
int* const x = new int
You now are able to change the contents of x
*x = 21;
It is not possible the change the address x is pointing to.
x = (int *)&MAX;
This will generate an error. See function Test3()

				
					void Test3() {
	const int MAX = 42;
    int* const x = new int;
	// dereferencing x and assign a new value is possible
	*x = 21; 
	// Changing the pointer to point to another address is not possible
	// x = (int*)&MAX; // ERROR
	std::cout << *x << std::endl;
}
				

There is another position where we can place the const keyword
int const* x = new int;
This is the same as
const int* x =new int;
Remember that when the const keyword is before the pointer sign (*) it doesn’t matter if you use
int const* x = new int or const int* x = new int
The last option that you can use is
const int* const x = new int
Now you cannot change the contents of the pointer or the pointer itself (see function Test4() ).

				
					void Test4() {
	const int MAX = 42;
	int* const x = new int;
	// dereferencing x and assign a new value is possible
	*x = 21;
	// Changing the pointer to point to another address is not possible
	// x = (int*)&MAX; // ERROR
	std::cout << *x << std::endl;
}
				

Applying const to a class

Let’s create a Coordinate class with two members x and y.
For changing and reading x and y we add the SetX, SetY and GetX and GetY methods.

The following declaration
int getX() const

Ensures the x cannot be modified within the scope of this function and make getX a readonly function.

This enable us to write a display function with using a const reference to prevent the Coordinate instance to be copied and this safe time and resources.

Here is the function signature of the Display function:

void DisplayCoordinate(const Coordinate& point)

This function signature forces the GetX and GetY function to be declared as const. If you remove the const keyword from GetX you are allowed to make changes and cannot pass point by const reference. If there are cases where you need GetX to be modified you can define it as a class member without the const keyword. The C++ compiler will sort out for you which GetX version to use (Test5() )

				
					class Coordinate {
private:
	int x, y;
public:
	int GetX() const {
		// x = 42 gives an error  
		return x;
	}
	void SetX(int val) {
		x = val;
	}
	int GetY() const {
		return y;
	}
	void SetY(int val) {
		y = val;
	}
};
void DisplayCoordinate(const Coordinate& point) {
	std::cout << point.GetX() << ", " << point.GetY() << std::endl;
}
void Test5() {
    	Coordinate point;
    	point.SetX(0);
    	point.SetY(42);
    	DisplayCoordinate(point);
}