Creating a multidimensional array in C++/CLI with

2019-08-15 03:34发布

问题:

I am working on converting a C# project to a C++/CLI project. I came across this code and wanted to verify that I am using the proper C++/CLI syntax. I am pretty sure I am doing it wrong, just setting parameters when I want to set the size of the dimensions.

Original C#:

public double[][] _ARRAY = new double[num][];

C++/CLI:

array<double, 2>^ _ARRAY = gcnew array<double, 2>{ {num}, {} };

回答1:

That IS how you create a multidimensional array in C++/CLI. But the C# isn't actually a multidimensional array at all.

These two are the same:

/* C# */ public double[][] arrayOfArray;
/* C++/CLI */ array<array<double>^>^ arrayOfArray;

and so are these:

/* C# */ public double [,] array2D;
/* C++/CLI */ array<double,2>^ array2D;

A real two-dimensional array can't be half-dimensioned as you show, that's only possible with a jagged array (array of arrays). For the jagged array, C++/CLI should certainly allow

arrayOfArray = gcnew array<array<double>^>(num);

which is (just like the C# code in your question) an array of (initially null) managed handles to arrays.



标签: .net c++-cli