Q:

Behaviour about const_cast<>

I have written a small problem for checking the behavior of const_cast on const data member.
using namespace std;

     class myString{
         public:
                 myString(char * str)
                 {
                         p=str;
                 }
                 const char * getString(){
                         return p;
                 }
         private:
                 const char *p;
 } ;

int main()
{
        char *p=(char*)malloc(8);
        cin>>p;
        myString *m= new myString(p);
        char *s =const_cast<char*>(m->getString());
        s[6]='y';
        cout<<s<<endl;
        return 0;
}

After running this program I give the out as "yogendra"  (a 8 letter string). and i got the output as "yogendya"
Now my doubt. Through const_cast<> we can override the behavior of the data member itself as here the string is const char* still after casting i can modify it.

A:

You've described exactly what const_cast is for - it allows you to removed the constness from something and modify it.  It's up to you not to abuse that power.  :-)
(In your case this doesn't apply, but note that you can cause crashes by using const_cast - for example:
const char *c = "Hello";
char *s = const_cast<char*>(c);
s[0] = 'h';

could crash because the compiler can put the string literal into read-only memory.)

