C++ const_cast неожиданный результатC++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 C++ const_cast неожиданный результат

Сообщение Anonymous »


Out of curiosity I executed that simple code:

#include int main() { const int cValue{32}; const int* pcValue = &cValue; int* pValue = const_cast(pcValue); *pValue = 16; assert(&cValue == pcValue); assert(&cValue == pValue); assert(*pcValue == 16); assert(*pValue == 16); assert(cValue == 16); // Ooops! cValue is 32. } I can see that pValue and pcValue both points on the same memory address as &cValue, but, interestingly, changing the value by that address doesn't affect cValue (at the same time Memory Viewer states that a corresponding memory block was modified). Just interesting how it works and is this a UB or not.


Источник: https://stackoverflow.com/questions/780 ... ng-outcome
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»