Re: static virtual functions
* kiran:
Why cannot a static member function of class be declared as virtual?
Because the language doesn't make use of that combination.
In other words, there was no apparent natural definition that was deemed
important enough to be worth the work and added language complexity.
Other replies, perhaps appearing before this article, will probably
(incorrectly) tell you that it's "impossible".
One reasonable use for the combination "static virtual" is the case of a
virtual function that does not access per-instance data, such as a
function className:
struct ShubbiDua
{
virtual ~ShubbiDua() {}
virtual std::string className() const { return theClassName(); }
static std::string theClassName() { return "ShubbiDua"; }
};
"static virtual" could have been defined so that this could be written
simply as
struct ShubbiDua
{
virtual ~ShubbiDua() {}
static virtual std::string className() { return "ShubbiDua"; }
}
But consider: how often do you need this, and what are the savings?
Another reasonable definition of "static virtual" could be as a virtual
function in a Smalltalk-like meta-class for the class in question. Then
add in Java-like static constructors, and so on. The whole machinery.
That could yield a very pleasing orthogonality of concepts (where
everything can be applied to anything), and be useful for e.g. an
introspection facility as well as a language based anchor for object
factories used in e.g. de-serialization.
But again consider: how often do you need this, and what are the savings?
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]