I think the question might be answered if you asked a different question.
If I were to ask you why it should NOT be allowed, what would you say?
I would say that, were it not allowed, it would always be clear whether a
call was to a static member or a non-static member.
Part of what brought up this question for me was a passage in A Programmer's
Introduction to C# by Eric Gunnerson regarding calls to static methods
(p.66): "This is unlike the C++ behavior where a static member can be
accessed either through the class name or the instance name. In C++, this
leads to some readability problems, as it's sometimes not clear from the
code whether an access is static or through an instance."
By the way, I sent a reply to Janusz, but accidentely sent it to him in
person instead of to the newsgroup. Here is my reply and his reply to that
reply. (I removed the offending button in my newsreader ;-).)
Thanks for your insights.
Joost
Isn't it the other way around? Static member functions operate on the class,
not the object, so you have to write object::static_function() but for
convenience you can also write instance.static_function(). I know C# doesn't
allow invoking a member function on an object instance: you have to use the
class so that it is always clear you are calling a static member function.
This triggered my original question: if there is an explicit reason the
designers of C# don't allow static member invocation through an instance,
then the designers of C++ must also have a reason why they do allow static
member invocation through an instance, but what is it?
Hard to speak in the name of C++ designers, however in the C++ standard
(9.4/2) one may find the following statement:
"A static member s of class X may be referred to using the qualified-id
expression X::s; it is not necessary to use the class member access
syntax. (5.2.5)"
5.2.5 says about . -> type access
Some paragraphs before discuss class member access in general and from
that perspective one may think of static members as a special case
following the general concept as close as possible.
AFAIK static member functions were not present in C++ from the beginning
and were added somewhat later. So they were expected not to introduce
too many changes.
So I think it may be the reason of differences between C++ (and Java I
think) and C#. The first attitude focuses on keeping language concepts
consistent, fitting static member functions in the scope of member
functions and the second keeps clean programming style in mind.
Regards,
Janusz