What am I missing here? I thought that the ^ was a smart pointer and that I could pass string by ref to a function. What about other hat objects?
// calling code
MyClass::GetString(_str1, _str2);
// both strings are nullptr at this point
/* static */
MyClass::GetStrings(String^ str1, String^ str2)
{
// Read from Local Settings
auto value = localSettings->Values->Lookup(kKey);
String^ temp = ref new String(value->ToString()->Data());
str1 = temp;
// same for str2
}
A
T^is similar to a pointer type; when you have a function with aT^parameter (like yourGetStringsstatic member function), the hat is passed by value, just like a pointer is passed by value.If you want the
String^‘s passed by reference, you need to use references:Note that references are a C++ language feature and are not part of the Windows Runtime type system. The Windows Runtime type system also does not support in/out by reference parameters; only out parameters are supported.
Therefore, if
MyClassis a public Windows Runtime reference type andGetStringsis a public or protected member function (basically, ifMyClass::GetStringsappears in metadata and is callable across the Windows Runtime ABI), you need to use pointers:These pointers are out parameters: you can write to the pointed-to
String^s but you cannot read from them.