C++ Tutorial – 26 – Constants
A constant is a variable that has a value which cannot be changed once the constant has been assigned.
Constant variables
A variable can be made into a constant by adding the const keyword either before or after the data type. This modifier means that the variable becomes read-only, and it must therefore be assigned a value at the same time as it is declared. Attempting to change the value will result in a compile-time error.
const int var = 5; int const var2 = 10; // alternative order
Constant pointers
When it comes to pointers, const can be used in two ways. First, the pointer can be made constant, which means that it cannot be changed to point to another location.
int myPointee; int *const p = &myPointee; // pointer constant
Second, the pointee can be declared constant. This means that the variable pointed to cannot be modified through this pointer.
const int *q = &var; // pointee constant
It is possible to declare both the pointer and the pointee as constant to make them both read-only.
const int *const r = &var; // pointer & pointee constant
Note that constant variables may not be pointed to by a non-constant pointer. This prevents programmers from accidentally rewriting a constant variable using a pointer.
int *s = &var; // error: const to non-const assignment
Constant references
References can be declared constant in the same way as pointers. However, since reseating a reference is never allowed, declaring the reference as const would be redundant. It only makes sense to protect the referee from change.
const int& y = var; // referee constant
Constant objects
Just as with variables, pointers and references, objects can also be declared constant. Take the following class as an example.
class MyClass { public: int x; void setX(int a) { x = a; } };
A constant object of this class cannot be reassigned to another instance. The constness of an object also affects its fields and prevent them from being changed.
const MyClass a, b; a = b; // error: object is const a.x = 10; // error: object field is const
Constant methods
Because of this last restriction, a constant object may not call a non-constant method since such methods are allowed to change the object’s fields.
a.setX(2); // error: cannot call non-const method
They may only call constant methods, which are methods that are marked with the const modifier before the method body.
int getX() const { return x; } // constant method
This const modifier means that the method is not allowed to modify the state of the object and can therefore safely be called by a constant object of the class. More specifically, the const modifier applies to the this pointer that is implicitly passed to the method. This effectively restricts the method from modifying the object’s fields or calling any non-constant methods in the class.
Constant return type and parameters
In addition to making a method constant, the return type and method parameters may also be made read-only. For example, if a field is returned by reference instead of by value from a constant method it is important that it is returned as a constant in order to maintain the constness of the object. Not all C++ compilers will be able to catch this subtle mistake.
const int& getX() const { return x; }
Constant fields
Both static and instance fields in a class can be declared constant. A constant instance field must be assigned its value using the constructor initialization list. This is the same as the preferred way of initializing regular (non-constant, non-static) fields.
class MyClass { public: int i; const int c; MyClass() : c(5), i(5) {} }
A constant static field on the other hand must be defined outside of the class declaration, in the same way as non-constant static fields. The exception to this is when the constant static field is of an integer data type. Such a field may also be initialized within the class at the same time as the field is declared.
class MyClass { public: static int si; const static double csd; const static int csi = 5; }; int MyClass::si = 1.23; const double MyClass::csd = 1.23;
Constant guideline
In general, it is a good idea to always declare variables as constants if they do not need to be modified. This makes sure that the variables are not changed anywhere in the program by mistake, which in turn will help to prevent bugs.
If you like this tutorial please +1 it:


![[Affiliate link] Total Training]( http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/totaltraining.png)
![[Affiliate link] Lynda](http://d3qzmfcxsyv953.cloudfront.net/images/pvt-affiliates/lynda.png)

