No. This question in NOT duplicate of When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?
The question asked here is no ways similar to the link described as duplicate.
First question : I am using const_cast below for two cases. one of it works. the other doesn't.
1. int* const //Works.
In this syntax the address to which the variable would point to cannot be changed. So i used const_cast as below and it works:
`
int j=3;
int *k=&j;
int *m=&j;
int* const i=k;
const_cast<int*>(i)=m; //OK: since i=m would not work so cast is necessary`
2. const int* //Doesn't work.
The address being pointed to can be changed however the value cannot be changed(though can be changed by making the variable point to different address). The const_cast i am using doesn't seem to work here:
`
int j=9;
int *k=&j;
const int* i1=0;
i1=k; //OK
//*i1=10;//ERROR.`
So i tried to typecast as below through various ways but nothing works:
const_cast<int*>(i1)=10;
const_cast<int*>(*i1)=l;
*i1=const_cast<int>(l);
*i1=const_cast<int*>(10);
Second question: Are all the casts available only for pointers and references? Is the following example not valid where no pointer or reference is in picture?
const int a=9;
int b=4;
const_cast<int>(a)=b; //cannot convert from 'int' to 'int'. why is compiler
//trying to convert from int to int anyways or fails
//when both the types are same.