Learning  C++
Home
Tutorials
C++  Programs
Contact  us
Sitemap
// Program to illustrate the const_cast operator

#include<iostream.h>
class Example
{
     private:
     int n;
     public:
     int get() const
     {
          return n;
     }

     void set(int x)
     {
          n=x;
     }

     void change() const
     {
          const_cast <Example *> (this)->n++;
     }
};


void main()
{
     Example obj;
     obj.set(6);
     cout<<"\n Before applying cast operator the value of a is :"<< obj.get();
     cout<<"\n After applying cast operator the value of a is :";
     obj.change();
     cout<<obj.get();
}



Test data

Before applying cast operator the value of a is : 6
After applying cast operator the value of a is : 7