const is a terrible cancer and should be stopped in its tracks!
so, in C++, there is an infectious storage class modifier called const. const is evil
because it spreads unstoppably through code. Have a class with a const member? have to ever pass
it as an argument to a function (oh, sorry, they call those methods now)? well you'll have to
pass it in as const (and of course mangle your function in the process).. Can't mutilate
the function (like say it doesnt belong to you)? well sorry, you can't cast out of const-ness (const_cast kind of works but will sometimes not).
One of the most evil aspects of const is that it infects any clean C code
that you might be trying to compile or even link into a c++ program.
(why on earth would one want to keep as much of the code as possible as
clean simple C, we will leave as an exercise for the reader :-)
Need to work around this lunacy? quite simple! the compiler is clever, but not clever enough
to see through unions! (or any other equivalent pointer wankery that could get a normally
declared pointer pointing at the same location as a const one)
It has the slight advantages over simple pointer wankery that unions are much more portable,
a good bit more transparent to a human reading the code, and don't/shouldn't open the door
for any other compiler complaints.
just do something like this:
const char* constipated;
and your life will be much happier.
now when you need to free up some data that's imprisoned in const-land,
do something like this:
const char* blorf = "skata"; /* ptr const'ed here */
char* foo; /* but we want to use this */
Note that C++ won't let you create a union which has any element which has a
constructor, so you can't put arbitrary class instances (like string!) in a union.
However, the reason you're using this is to work around const and pass a pointer ,
right? so why would your union ever have anything other than basic data types in it?
(or pointers to other fancier C++ish things) ;)
This file and all other content on the VAXpower.org site copyright 2008 by G. Economou