【问题标题】:c++ initialize values static int with CLI arrayc++ 使用 CLI 数组初始化静态 int 值
【发布时间】:2015-09-05 20:58:07
【问题描述】:

所以我有 2 个数组都是二维 cli::arrays。 初始化 cli::array 的正确语法是什么。我在下面的示例中进行了尝试,但不起作用。

//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

【问题讨论】:

  • .NET 数组总是初始化为零。您可以使用“类型初始化器”来预加载其他值。
  • 你能举个例子吗?
  • 参见 MSDN 上的示例:msdn.microsoft.com/en-us/library/…
  • @saito ,不要将静态字段私有化,并在不同的范围(区域)中承载该字段。并在将值赋值给变量之前删除 int。

标签: c++ arrays class static command-line-interface


【解决方案1】:

所以我用静态构造函数解决了这个问题,我注意到你应该输入 [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);
};

【讨论】:

    猜你喜欢
    • 2021-01-31
    • 1970-01-01
    • 2010-09-13
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多