C キャスト 新しいページはコチラ
提供: yonewiki
(→キャスト) |
(→キャスト) |
||
63行: | 63行: | ||
pconstnLimit = &nLimit; | pconstnLimit = &nLimit; | ||
+ | ※上記のconst付きのポインタはポインタのアドレスは変更できるconstで、アドレスは何度でも入れ替えることができます。アドレスが指し示す中身を変更できないようにするものです。 | ||
+ | アドレス自体を変更させないconstにする場合は、int* const pconstnLimit = &nLimit; のように *の後ろにconstを付与します。 | ||
+ | |||
+ | const int* const pconstnLimit = …;とすると中身もアドレスも変更できなくなります。値の中身の変更を禁止するcointは 型名の後ろに書くことができますので、 | ||
+ | |||
+ | int const * const pconstnLimit = …;とも書けます。どちらにしてもあまり見かけない使い方だとは思います。C++の難しさはこのあたりにあると思う。こうやって書いてもいいという曖昧さ。柔軟さ? | ||
*const外し | *const外し | ||
77行: | 83行: | ||
*static_cast 変換 | *static_cast 変換 | ||
*reinterpret_cast 変更 | *reinterpret_cast 変更 | ||
− | *const_cast | + | *const_cast const外し |
*dynamic_cast | *dynamic_cast | ||