2011年6月3日金曜日

const 型へのポインタの delete

こういう const 型ヘのポインタを delete するのって合法なんですね.

const hoge* ptr = new hoge;
delete ptr;                     // OK

5.3.5 delete のとこに書いてた.

[Note: a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression. ]

いまいち釈然としないなーと思ってテケトーにぐぐる先生に聞いてみてたら ここの説明でしっくりきた.たしかにスタックに const なオブジェクト構築 した場合でもデストラクタ呼べないかんし,そういうもんなんやね

http://stackoverflow.com/questions/755196/deleting-a-pointer-to-const-t-const

0 件のコメント:

コメントを投稿