Constant members
A constant is a value which can not be modified. A constant is defined with the keyword const. Once such a value is defined, its value can't be changed.
A class member can be defined as a constant. Even a method of a class can be defined as constant. Let us find out what are these.
Constant Data Members of a class
A class can have constant data members by using keyword const in declaration of these. Such members can not be modified once the object is created.
If that is the case, how do we initialize such const members?
Let us look at an example with constant data member.
The above class does not compile. The data member nsides is defined as constant. So it can not be modified. It can not be changed even within the constructor.
That is why the statement in the constructor
nsides = 3;
gives compiler error.
The solution is initializing const data members using member initializer list.
Now let us look at the correct method of initializing our nsides variable in Triangle class.
As can be seen in the example, const member nsides is initialized in member initializer list in the header of the constructor as nsides(3) - nsides is initialized to 3.
So if a class has a constant data member, we have to
-
write a constructor for that class
-
in the constructor, initialize the const member using member initializer list.
Constant Objects
We use constant PODs (plain old data types like int, char etc). Can we have constant objects?
Yes, we can define a constant objects using the keyword const.
Once defined, the state of these constant objects can not be modified. That is - their data members can not be modified either directly or indirectly using member functions.
Let us look at an example.
The object obj1 is a constant object and it can not be modified. So obviously it can not call the function seta() because it changes a.
But even the call obj1.print() produces error. Why? Why does the call produce an error when print() function does not modify any data members?
The reason is, compiler allows constant object to call only constant member functions - functions which are explicitly declared as constant using keyword const. Such functions can not modify state of the object.
Constant functions
A constant member function is a function of a class which guarantees that it does not modify the state of the object i.e. it can not modify any data member.
A constant function is written by adding keyword const after the parameter list. This const key word should be added both in declaration of the function and definition.
As you can see in the modified code above, print() function, which is now a constant function, is not allowed to change a - the data member. Trying to modify a in print() function gives a compiler error. (the commented line)
obj1 which is a constant object can call this constant function print(). But this object can not call seta() function because it is not constant function.
As can be expected, a non-constant object obj2 can call both constant and non-constant function.
We can overload a function using constantness. That is - there can be two member functions which are similar in name and parameter lists, but one is constant and other is not constant.
e.g.
class A
{
public:
void print();
void print() const;
};
Note :
-
Constant member functions are called inspectors as they can't modify the object - where as non-constant functions are called mutators.
-
Member functions that are static can't be declared to be const. The const keyword modifies the this pointer that is passed to a member function, but static member functions don't a this pointer since they can be called without an object.
-
Constructors and destructors can never be constant as they are always allowed to modify the object.
Mutable members:
Even from a constant object, mutable data members can be modified and they can be changed even inside a constant functions.
That means if a member is defined using keyword mutable, it can be changed even within a const function.
Let us look at an example.
In the code above, num_accessed is incremented within a constant function and the program would compile without any errors.