Topic: static virtual (Was:Multiple Dispatch -- Better Than Item 31?) (http://www.pop.de)
Author: "J.Barfurth" <techview@bfk-net.de>
Date: 1999/11/18 Raw View
[added comp.std.c++ as this goes more towards the language lawyers]
<merlinvin@my-deja.com> schrieb in im Newsbeitrag:
80rvhs$4tk$1@nnrp1.deja.com...
> 2) Why _can't_ you have a virtual static function? There must be a good
> reason, but I have no clue as to what that good reason is.
Contrary to what others have answered, I think the language could allow it.
There may be a rationale for not allowing it, but it escapes me.
The idea is to have per-class behaviour be dispatched on an per-object
basis.
Actually there is one case where we do have a virtual static function in the
language: operator delete
The strange thing about that one is only, that you state whether it should
be 'virtual' at the declaration of the destructor.
By analogy one could allow the following:
<EXAMPLE>
struct A
{
virtual static char* name() { return "A"; }
};
struct B
{
virtual static char* name() { return "B"; }
};
int main()
{
A a;
B b;
std::cout << A::name() << B::name() << std::endl;
std::cout << a.name() << b.name() << std::endl;
// now for polymorphic dispatch
A* poly;
poly = &a;
std::cout << poly->name();
poly = &b;
std::cout << poly->name() << std::endl;
}
</EXAMPLE>
Which should print:
AB
AB
AB
I had a need for this behaviour more than once and find the usual workaround
more error-prone:
struct A
{
static char* static_name() { return "A"; }
virtual char* name() { return static_name(); }
};
struct B : A
{
static char* static_name() { return "B"; } // might forget this - it
could be needed in a template
virtual char* name() { return static_name(); } // might forget this,
if not pure in A
};
This is also more difficult to read, as you have to think up another name
for the same behaviour. The requirement that static_name() and name() return
the same value for a most derived object cannot presently be expressed in
the language. The compiler could just generate code like the above (without
the unneeded implicit this argument).
The dispatch of this also resembles the way typeid is resolved:
if called for a type do static (compile-time) dispatch
if called for an object do dynamic (runtime) dispatch
It would even be possible to have a pure static virtual. As usual it would
have to be defined if called directly.
-- J rg Barfurth
To comp.std.c++: should this be considered for the distant next version of
the standard ?
What do you think ?
[ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]