2015-06-19 51 views
3

所以我有2个数组都是2维cli :: arrays。 初始化cli :: array的正确语法是什么?我在下面的例子中尝试过,但那不起作用。C++使用CLI数组初始化静态int值

//Cords.h 
ref class Cords { 
private: 
    static array<int,2>^ Xcord = gcnew array<int,2>(4,4); // [4][4] 
    static array<int,2>^ Ycord = gcnew array<int,2>(4,4); // [4][4] 
public: 
    Cords(); 
    static int getX(void); 
    static int getY(void); 
}; 
int Cords::Xcord[0][0] = 4234; //On these lines is the mistake 
int Cords::Ycord[0][0] = 2342; //On these lines is the mistake 
+0

.NET数组总是初始化为零。您可以使用“类型初始化程序”来预加载其他值。 –

+0

你能举个例子吗? – delorax

+0

查看MSDN上的示例:https://msdn.microsoft.com/en-us/library/ke3a209d.aspx#BKMK_Static_constructors –

回答

0

所以我解决了静态构造函数的问题,我发现你应该键入[0,0]而不是[0] [0]。我习惯于正常的C数组。

//Cords.h 
ref class Cords { 
private: 
static array<int,2>^ Xcord = gcnew array<int,2>(4,4); // [4][4] 
static array<int,2>^ Ycord = gcnew array<int,2>(4,4); // [4][4] 
static Cords() {   //static constructor to initialize values 
     Xcord[0,0] = 4234; // [0,0] instead of [0][0] 
     Ycord[0,0] = 2342; 
     ... 
    } 
public: 
    Cords(); 
    static int getX(void); 
    static int getY(void); 
};