const casting an int in a class vs outside a class
<p>I read on the wikipedia page for <a href="http://en.wikipedia.org/wiki/Null_pointer#Null_pointer" rel="nofollow">Null_pointer</a> that Bjarne Stroustrup suggested defining NULL as </p>

<p>const int NULL = 0;</p>

<p>if "you feel you must define NULL." I instantly thought, hey.. wait a minute, what about const_cast?</p>

<p>After some experimenting, I found that</p>

<pre><code>int main() {
    const int MyNull = 0;
    const int*  ToNull = &amp;MyNull;
    int* myptr = const_cast&lt;int*&gt;(ToNull);
    *myptr = 5;
    printf("MyNull is %d\n", MyNull);
    return 0;
}
</code></pre>

<p>would print "MyNull is 0", but if I make the const int belong to a class:</p>

<pre><code>class test {
public:
    test() : p(0) { }
    const int p;
};
int main() {
    test t;
    const int* pptr = &amp;(t.p);
    int* myptr = const_cast&lt;int*&gt;(pptr);
    *myptr = 5;
    printf("t.p is %d\n", t.p);
    return 0;
}
</code></pre>

<p>then it prints "t.p is 5"!</p>

<p>Why is there a difference between the two? Why is "*myptr = 5;" silently failing in my first example, and what action is it performing, if any?</p>
