reinterpret_cast casts away qualifiers

Seb picture Seb · Jan 17, 2015 · Viewed 35.4k times · Source

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

int ProgressBar(const uint64_t data_sent, const uint64_t data_total, void const *const data)
{
    Dialog *dialog = reinterpret_cast<Dialog*> (data);
    dialog->setValue((data_sent *100) / data_total);
}

the reinterpret_cast seems not allowed and say

reinterpret_cast from 'const void *) to Dialog * casts away qualifiers

Any idea

Answer

Pranit Kothari picture Pranit Kothari · Jan 17, 2015

As Nick Strupat stated in comment,

reinterpret_cast can't cast away cv-qualifiers

So you can use reinterpret_cast and const_cast together.

Dialog *dialog = const_cast<Dialog*>(reinterpret_cast<const Dialog *>(data));