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.