A Long Long Shadow

        Submitter: Oleg Goldshmidt

        Language: C++

        Date:        2003/07/16

I looked at a piece of code yesterday that boiled down to the following (this is really stripped down to bare necessities, it was more complicated in real life):

class Object
long shadow;
static void Cast(long shadow);

void Object::Cast(long shadow)
shadow = 1L;

(OK, you have noticed that shadow is merely long, not long long as the title promised. Consider though that ISO C++ does not support long long - I simply used a bit of artistic license here). 

If you try to compile it the compiler will (or should) complain that the parameter shadow of Object::Cast() in fact shadows a member of Object. So, big deal, you say, it's just a stupid compiler warning. Let's just rename the parameter to, say, shadow_.

What, however, will you do with the shadow that is in the body of the function? Certainly there is no sense to change that to shadow_, which will lead to assignment to an argument passed by value. So, you may decide, maybe the original intention was that the member of Object should be assigned to, and the shadowing is in fact a bug caused by automated global renaming or some other silly trick like that?

Think again. A static member function cannot assign to a non-static member. The code does not make sense either way.