Quantcast
Channel: User Peter - Reinstate Monica - Stack Overflow
Viewing all articles
Browse latest Browse all 223

Comment by Peter - Reinstate Monica on C++ const_cast surprising outcome

$
0
0
The reason this is undefined behavior is that a truly const object may well end up in read-only memory on some (for example, embedded) systems! This is not the case on a "normal computer" though; the const object exists in memory (because its address has been taken) and that memory is perfectly writable. The reason for the discrepancy you see between the contents of that memory location and what the compiler substitutes for cValue is that the compiler is free to assume that the value of cValue (a const object, after all!) never changes. The compiler probably simply inserts 16 there.

Viewing all articles
Browse latest Browse all 223

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>