Sunday, September 29, 2013

Best Places to use Const

“const” is the keyword which comes very handy saving much of our time finding bugs and aiding in writing correct code. If there is a keyword provided from the compiler which makes our life easier , why not to use it. But most of the time , we are confused about its usage . Below are the places where const “should” be used .

The behavior of const is known thing that if we make an object const , compiler puts a constraint on your code that it shouldn’t be modified.

Following is the semantics of const usage:

  • const char *p = "C_PLUS_CPLUS"                 //here  pointer is not constant but the data is constant
                                                   //You can increment decrement the pointer
                                               //but you cannot change the data
 
     As such 
            p++ ;   p += 2;                    //Valid statement as Pointer is  not const
 
     //But if you do this 

     p[1] = 'D'; //Invalid because data is constant






  • char * const p = “C_PLUS_CPLUS"               //here the pointer is constant but not the data
    p++ ;                         //Invalid statement

    p[0] = 'D'                   //Valid as data can be changed.





  • const char * const p = "C_PLUS_CPLUS"               //Here not even the data in constant but even teh pointer is constant

As we can we figure out the syntax , if const is put before variable name after the asterix (“*”) , variable in const not the memory it owns in const , but if the const is placed at the left side of asterix , then the data which the variable owns in const.


Const in the Function Parameters


Most of the time const is placed as the return value of a function as


const Complex operator+(const Complex& C1, const Complex& C2);






Const should be specified in the function parameters , which does not change the parameters, When you know that you will not change the parameters inside the function body you should specify those as const , so as to avoid any mistake of changing them.


NOTE:: Above two parameters are passed as reference is a separate topic , which wiill be discussed shortly.


Const in Function Return type


In the above function declaration , return value is returned as const , because to handle following reasons.


If it was not there , any programmer can do it like this


Complex c1,c2,c3;
c1 + c2 = c3



const is specified in return type just to avoid these kind of issues.


Const Member function


We have seen const in function parameters , in function return values , but there is third situation where const is used ,  most of us of unaware of it and beginners mostly hesitate to write it

//Consider a ArrayList containg the array of 100 chars
Class ArrayList
{
     private:
            char m_cData[100];
   public:
        const int & opeator[](int  nPosition)  const    //operator [] for const ArrayList Objects
        {
                           return  m_cData[nPosition];
         }
        int & operator[](int nPosition)                //operator [] for non-const ArrayList objects
       {
                          return m_cData[nPosition];
        }
};

//Now the Above ArrayList object can be used like this


ArrayList arrList("C_PLUS_PLUS");
std::cout<<arrList[0];                                 //calls non-const member function ArrayList::operator[]
 
const ArrayList arrList("C_PLUS_PLUS");
std::cout << arrList[0];                             //calls const member function ArrayList::operator[]



 


Now you would be asking why to write a const member function when there is a equivalent function for the same purpose.


Its because there will be functions which are accepting parameters as a const – reference , so if you have written a const member function , the const objects passed as a reference in the function , which when used in the function calls their own version of const member functions.

No comments:

Post a Comment