Получаю ошибку при попытке приведения типов
Написал вот такой код:
static int cmp(const void* a, const void* b) {
return( ((massifVoltageAndIndex*)a)->Voltage -
((massifVoltageAndIndex*)b)->Voltage );
}
При сборки получил предупреждение "old-style cast" попытался исправить на
static int cmp(const void* a, const void* b) {
return( (reinterpret_cast<massifVoltageAndIndex*>(a))->Voltage -
(reinterpret_cast<massifVoltageAndIndex*>(b))->Voltage );
}
Но получил ошибку:
error: reinterpret_cast from type ‘const void*’ to type
‘massifVoltageAndIndex*’ casts away qualifiers
return( (reinterpret_cast<massifVoltageAndIndex*>(a))->Voltage -
(reinterpret_cast<massifVoltageAndIndex*>(b))->Voltage );
^
Ответы (2 шт):
Автор решения: Harry
→ Ссылка
Кушаем слона по кусочку :)
static int cmp(const void* a, const void* b)
{
return ((reinterpret_cast<massifVoltageAndIndex*>(const_cast<void*>(a)))->Voltage -
(reinterpret_cast<massifVoltageAndIndex*>(const_cast<void*>(b)))->Voltage);
}
Автор решения: user7860670
→ Ссылка
Здесь нет ни одной причины использовать C-style, reinterpret или const кастование:
static int cmp(const void* a, const void* b)
{
using Ptr = massifVoltageAndIndex const *;
return static_cast<Ptr>(a)->Voltage - static_cast<Ptr>(b)->Voltage;
}