SE450: Overriding: C++/C# [21/35] |
C++ and C# default to static dispatch, for all functions, including member functions.
Boxed entities are manipulated using
references/pointers, live java objects.
Unboxed entities are manipulated as values, like
java base types.
You can convert between them using wrapper objects.
Integer box(int v) { return new Integer(v); } int unbox(Integer r) { return r.intValue(); }
Java 1.5 does this automatically in most contexts.
In C#, instances of a class are boxed; instances of a struct are unboxed --- structs generalize Java's base types.
To get dynamic dispatch, use the keyword
virtual
, which is allowed only in classes
(not structs).
If you are interested in C#, see here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/csspec/html/vclrfcsharpspec_11_3.asp
In C++, objects of any type may be boxed or unboxed. For example:
string v = string("cat"); // unboxed string* r = new string("dog"); // boxed int v = 1; // unboxed int* r = &v; // boxed
To get dynamic dispatch, the receiving object must be boxed and the method must be virtual. See file:dynamicVersusStaticDispatch.cpp [source].