reinterpret_cast casts away qualifiers

后端 未结 2 798
囚心锁ツ
囚心锁ツ 2021-02-02 07:01

I add an issue on reinterpreting a variable and I don\'t know why..

int ProgressBar(const uint64_t data_sent, const uint64         


        
2条回答
  •  太阳男子
    2021-02-02 07:29

    You need to also use a const_cast to remove const qualifiers. Also, casting from void * can use static_cast, it does not need to reinterpret. For example:

    Dialog const *dialog = static_cast(data);
    Dialog *d2 = const_cast(dialog);
    

    However , make sure that the Dialog is actually not a const object; attempting to modify a const object (presumably setValue does this) causes undefined behaviour.

    I'd suggest rethinking the interface to ProgressBar to avoid needing this cast.

提交回复
热议问题