Hi,
We had a discussion on this board regarding the usefulness of const_cast.
"if a variable is const at its point of definition, the result of
casting it away are undefined".
Polymorphic OOP suggested that this rule might not be applicable on non-static const datamembers.
Below mentioned is the extract of the email-interaction between "Bjarne Stroustrup" and me
---------------- My Question --------------------
Sir,
You have suggested that
const int i = 10;
int *p = const_cast<int *>(&i);
*p = 20; // Result is undefined
"if a variable is const at its point of definition, the result of
casting it away are undefined".
My question is "are datamembers an exception to to this suggestion?"
---------------------------My Question Ends Here ---------------
-------------- Bjarne Stroustrup's Answer ----------------------
No, and it's not a "suggestion" its a rule of the language. An
implementation is allowed to place a const in unwritable memory, such as a code segment, so that the *p=20 causes a run-time hardware exception. This is sometimes done in embedded systems where RAM is scarce and ROM cheaper.
-------------- Bjarne Stroustrup's Answer Ends Here--------------
Hope this is of some help to you folks