In C++/CLI, how do I declare and call a function with an ‘out’ parameter?

C++/CLI itself doesn’t support a real ‘out’ argument, but you can mark a reference as an out argument to make other languages see it as a real out argument.

You can do this for reference types as:

void ReturnString([Out] String^% value)
{
   value = "Returned via out parameter";
}

// Called as
String^ result;
ReturnString(result);

And for value types as:

void ReturnInt([Out] int% value)
{
   value = 32;
}

// Called as
int result;
ReturnInt(result);

The % makes it a ‘ref’ parameter and the OutAttribute marks that it is only used for output values.

Leave a Comment