Array and WriteOnlyArray parameters (C++/CX)
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
C++/CX supports authoring function parameters whose type is a read/write array (Platform::Array) or a write-only array (Platform::WriteOnlyArray). You can't define a WriteOnlyArray array, but you can declare a function parameter type as WriteOnlyArray so that the Windows Runtime can efficiently pass an Array by reference.
The following code fragment declares a read/write array of 10 integers, a function whose parameter is a read/write array, and a call to that function. Note that you can declare only 1-dimensional Array and WriteOnlyArray objects.
using namespace Platform;
Array<int>^ ai = ref new Array<int>(10);
for (int i = 0; i < ai->Length; i++)
ai[i] = i;
long MyFunc1(const Array<int>^ p1) {
long sum = 0;
for (int x = 0; x < p1->Length; x++)
sum += p1[x];
return sum;
};
MyFunc1(Array<int>^ ai);
The following code fragment declares a function whose first parameter is a WriteOnlyArray<T>. The function is called by using an Array<T> object even though the function parameter type is WriteOnlyArray<T>.
using namespace Platform;
void MyFunc2(WriteOnlyArray<int>^ a, int* actual) {
*actual = 0;
if (ai->Length > 2) {
a[0] = 0;
a[1] = 1;
*actual = 2;
}
};
int main() {
int used = 0;
auto ai = ref new Array<int>(5);
MyFunc2(ai, &used); // Array<T>^ has an implicit conversion to WriteOnlyArray<T>^
return used;
}