// vcmcppv2_explicit_boxing2.cpp
// compile with: /clr
using namespace System;
ref class A {
public:
void func(System::Object^ o){Console::WriteLine("in A");}
};
value class V {};
interface struct IFace {
void func();
};
value class V1 : public IFace {
public:
virtual void func() {
Console::WriteLine("Interface function");
}
};
value struct V2 {
// conversion operator to System::Object
static operator System::Object^(V2 v2) {
Console::WriteLine("operator System::Object^");
return (V2^)v2;
}
};
void func1(System::Object^){Console::WriteLine("in void func1(System::Object^)");}
void func1(V2^){Console::WriteLine("in func1(V2^)");}
void func2(System::ValueType^){Console::WriteLine("in func2(System::ValueType^)");}
void func2(System::Object^){Console::WriteLine("in func2(System::Object^)");}
int main() {
// example 1 simple implicit boxing
Int32^ bi = 1;
Console::WriteLine(bi);
// example 2 calling a member with implicit boxing
Int32 n = 10;
Console::WriteLine("xx = {0}", n.ToString());
// example 3 implicit boxing for function calls
A^ a = gcnew A;
a->func(n);
// example 4 implicit boxing for WriteLine function call
V v;
Console::WriteLine("Class {0} passed using implicit boxing", v);
Console::WriteLine("Class {0} passed with forced boxing", (V^)(v)); // force boxing
// example 5 casting to a base with implicit boxing
V1 v1;
IFace ^ iface = v1;
iface->func();
// example 6 user-defined conversion preferred over implicit boxing for function-call parameter matching
V2 v2;
func1(v2); // user defined conversion from V2 to System::Object preferred over implicit boxing
// Will call void func1(System::Object^);
func2(v2); // OK: Calls "static V2::operator System::Object^(V2 v2)"
func2((V2^)v2); // Using explicit boxing: calls func2(System::ValueType^)
}