puzzlecracker wrote:
I am sure you missed the introduction of templates in jdk 5.0 (1.5).
I'm sure I didn't, though you should be more careful than to refer to
Java generics as "templates." They're superficially similar, but
fundamentally different. A specialization of a generic does not really
create a new type, unlike a template specialization. Generics also
don't support specializing on constants, like templates do (e.g.
template <int x> identity() { return x; }). The types are erased after
compilation, which is pretty limiting. You can't overload on different
specializations of a generic type, because the types are erased after
compilation, meaning that at runtime all specializations revert to be
the same type as if you had never specialized them. Generics support
the "container of T" paradigm well, but for templates that's only the
barest beginning.
To get back to C++, and your question about run-time polymorphism and
virtual functions: remember that the only thing that makes a virtual
function different is their dispatch -- that is, when I have this code:
Parent * p = new Child();
p->doStuff();
The call to doStuff is dispatched to the Child implementation of that
function, if Parent declared doStuff as virtual. This is accomplished
by means of a virtual function table, or "vtable." The vtable is just
a lookup table, computed at compilation time. I'm not up on the
details of how the dispatch is accomplished, but you can look that up
on your own if you're interested.
A bit more material on static and dynamic typing:
http://en.wikipedia.org/wiki/Data_ty...dynamic_typing
Reading this, I find that my original definitions could use some
improvement. But most of what I've said about C++ still stands. The
gist of it is that in a dynamically-typed language, the type of a
variable may depend on the execution path on which it occurs. For
example:
if (cond) {
x = 5;
} else {
x = "foo";
}
To do something like this in C++, you'd have to write appropriate
conversion operators, assignment operators, or constructors, and you
still wouldn't be changing the type of x, just setting the value of x
based on a conversion from some other type.
Anyway, the Wikipedia article knows more than me, so you should
probably just read that.
Luke