いいえ。この質問は 、static_cast、dynamic_cast、const_cast、および reinterpret_cast を使用する必要がある場合の重複ではありません。
ここでの質問は、重複として説明されているリンクとはまったく異なります。
最初の質問: 以下の 2 つのケースで const_cast を使用しています。そのうちの1つが機能します。もう一方はそうではありません。
1. int* const //動作します。
この構文では、変数が指すアドレスは変更できません。だから私は以下のように const_cast を使用し、それは動作します:
`
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* //動作しません。
指しているアドレスは変更できますが、値は変更できません (ただし、変数を別のアドレスに指し示すことで変更できます)。私が使用している const_cast は、ここでは機能しないようです:
`
int j=9;
int *k=&j;
const int* i1=0;
i1=k; //OK
//*i1=10;//ERROR.`
だから私はさまざまな方法で以下のように型キャストしようとしましたが、何も機能しません:
const_cast<int*>(i1)=10;
const_cast<int*>(*i1)=l;
*i1=const_cast<int>(l);
*i1=const_cast<int*>(10);
2 番目の質問: すべてのキャストは、ポインターと参照に対してのみ使用できますか? 次の例は、ポインターまたは参照が画像にない場合は無効ですか?
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.