Constants in C++

Constant in C++ is a value which can not be modified. As in C, we can have literal constants using #define and we can have enums or we can define a variable to be const - fixed.  Let us concentrate on const variables here.

By declaring a variable as const, we ensure that it is not modified accidentlly. Any modification to constant will give a compilation error.

In the program below,  assignment to pi gives a compiler error because pi is defined as const.



int main()
{
    const float pi = 22.0/7;
    int radius = 12;
    radius++;/*ok*/
    pi = 3.14;/*error*/
}


Even function parameters can be constant. We have earlier discussed that making a reference parameter as constant is always better to avoid the function from modifying the argument.


void printnum(int & n)
{
    cout<<n++;
}
void printnum2(const int & n)
{
     cout<<n++;/*error*/
}
int main()
{
   int a = 10;
   printnum(a);
   cout<<a;/*prints 11*/
   printnum2(a);
}

In the code above, printnum() function increments the argument and it goes undetected by the compiler. Whereas, in printnum2(), n++ gives a compiler error, because the parameter is a constant reference, and so it can not be modified.

So unless your function intends to change the reference parameter, make it as constant reference parameter.

Now let us consider how to define and use constant members of a class.



Constant Data Members

A class can have constant data members by using keyword const in declaration. Such members can not be modified after the object is created.


The question arises - how do we initialize such const members?

Initializing const data members must be done using member initializer list. Because const data can not be assigned within constructor body.



class A
{
 const int n;
public:
 A();
};

A::A():n(22)
{
  n= 10;/*error*/
}


As can be seen in the example,  const member n is initialized in member initializer list. And assigning a value to n, even in the constructor, gives an error. 

So if a class has a const data member (a) write a constructor for that class (b) In the constructor, initialize the const member using member initializer list.

constant functions

A constant function CAN NOT modify the state of the object i.e. it can not modify any data member.

And constant objects can only call constant functions.

A const function is written by adding keyword const after the parameter list


class A
{
 int a;
public:
 void print() const;
 void seta(int m){a = m;}
};
void A::print() const
{
 cout<<a;
 // a = 0;/*invalid*/
}
int main()
{
 const A obj1;
 obj1.print();//valid
 obj1.seta(12);//error
 A obj2;
 obj2.seta(12);//valid
 obj2.print();//valid
}

As you can see in the code above, print() function, which is const, is not allowed to change - the data member. obj1 which is a constant object can call only const function viz. print(). It can not call seta() function. But a non-constant object obj2 can call both const and non-const function.

Constant objects

If an  object is declared as const, it can call only const functions. An ordinary object can call both const and non-const functions.

 

Mutable members:

Even from a constant object, mutable data members can be changed and they can be changed even inside a const functions.



class A
{
 mutable int num_accessed;/*mutable member*/
 int b;
public:
 /*other code*/
 void print() const;
};

void A::print() const
{
 cout<<num_accessed<<" "<<b;
 num_accessed++;/* valid. the member is mutable*/
}


In the code above, num_accessed is incremented within a const function and the program would compile without errors.

Comments

Popular Posts