Winterdom

In Managed C++, boxing and unboxing a value type instance were explicit operations (done with the __box operator and casts). In C++/CLI, however, things change. Boxing is now implicit:


Object^ boxedInt = 5;

This is one of the reasons why 0 is not a valid way to represent a null managed handle, since it would lead to an ambiguity (is Objecy^ p = 0; a boxed 0 value or a null handle?), and thus the introduction of nullptr (a much preferred alternative, in my humble opinion).

Unboxing still requires a cast (a boxing conversion), naturally:


int i = static_cast<int>(boxedInt);

As with MC++, there is a way to represent "typed" boxed values:


int^ boxedInt = 5;

In which case a simple dereference will do to unbox:


int i = *boxedInt;
.: tomasr | 2004-10-10 18:54:28 :.