C++/CX:Array and WriteOnlyArray parameters
翻译自MSDN文档:
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.
C++/CX支持编写参数为可读写数组(Platform::Array)或者是只读数组(Platform::WriteOnlyArray)的函数。你不能定义一个只读数组,但是你能声明函数参数为只读数组,以便windows runtime能有效的通过引用来传递数组。
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.
接下来的代码块声明了一个可读写的包含10个整数的数组,一个参数为可读写数组的函数,和这个函数的调用。记住你只能声明一维可读写数组和只读数组对象。
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>.
接下来的代码块声明了一个函数,第一个参数为WriteOnlyArray<T>。尽管函数参数是WriteOnlyArray<T>,但是函数还是被用Array<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; }