将数组初始化为固定长度数组的最佳方法是什么? (C ++ / CLI)

时间:2021-04-18 21:36:56

In managed C++/CLI, I could do this either as (1):

在托管C ++ / CLI中,我可以这样做(1):

array<System::Byte>^ css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};

or (2):

array<System::Byte>^ css_keycode;
css_keycode  = gcnew array<System::Byte>(6) {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};

But I apparently can't do (3):

但我显然做不到(3):

array<System::Byte>^ css_keycode;
css_keycode  = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};

even though I can do (4):

即使我能做到(4):

array<System::Byte>^ css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
array<System::Byte>^ css_keycode_shadow;
css_keycode_shadow = css_keycode;

Is there a better way that I'm missing? I'd like to have a simple/clean way to write somethiing like this:

有没有更好的方法让我失踪?我想有一个简单/干净的方式来编写像这样的东西:

public ref class decoder {
    array<System::Byte>^ css_keycode;
   ...
    decoder(void) {
        css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
    }
}

Thanks!

4 个解决方案

#1


You have to differ between initialization and assignment. It's like Tobias Wärre said in his post. You can't do (3) because assingnment does not work with the initialization brackets. (4) does work because you ardinarly assign the new values to your array. Actually the following should work:

初始化和分配之间必须有所不同。就像TobiasWärre在他的帖子中所说的那样。你不能这样做(3)因为assingnment不能用于初始化括号。 (4)确实有效,因为您将新值分配给数组。实际上以下应该有效:

public ref class decoder {
    array<System::Byte>^ css_keycode;
   ...
    decoder(void) {
        array<System::Byte>^ css_keycode_tmp = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
        css_keycode = css_keycode_tmp;
    }
}

This way the assigned values are copied to your array.

这样,指定的值将复制到您的数组中。

EDIT: Unfortunately there is no swap method like for STL containers (at least none I know of), else you could just swap the contents with the temporary.

编辑:不幸的是没有像STL容器那样的交换方法(至少我不知道),否则你可以用临时交换内容。

#2


You should be able to skip the size parameter when allocating the array with an aggregate initializer. For example, the following code compiles for me:

在使用聚合初始化程序分配数组时,您应该能够跳过size参数。例如,以下代码为我编译:

public ref class TestIt
{
public:
   TestIt()
   {
      mArray = gcnew cli::array<System::Byte>{0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
   }

private:
   cli::array<System::Byte>^ mArray;

};

Your example (3) doesn't work because the gcnew array<type> is required.

您的示例(3)不起作用,因为需要gcnew数组

#3


It seems like if you want to initialize an array like that

好像你想要初始化这样的数组

(array = {elem1, elem2,etc})

you need to do it at declaration. The declaration would also only have local scope, i.e. you cannot use the memory safely when exiting the function that declares the array, in that case you need to allocate the memory using new. In the latter case, do not forget to delete your object when it is no longer needed by the application.

你需要在申报时这样做。声明也只有本地范围,即退出声明数组的函数时不能安全地使用内存,在这种情况下,您需要使用new分配内存。在后一种情况下,不要忘记在应用程序不再需要时删除您的对象。

So 1 if you only need it locally or during a function call from that specific function and 2 if you need it elsewhere when the function has exited.

因此,如果您只需要本地或在特定函数的函数调用期间使用它,如果您在函数退出时需要其它函数则需要2。

#4


I think you're stuck with (2).

我认为你坚持(2)。

In C99, you could actually use compound literals to do (3), but I don't know if there's something like this in C++/CLI. It wouldn't help with your problem, anyway: using a compound literal in the body of a function would stack-, not heap-allocate the array.

在C99中,你实际上可以使用复合文字来做(3),但我不知道在C ++ / CLI中是否有这样的东西。无论如何,它对你的问题没有帮助:在函数体中使用复合文字将堆栈,而不是堆分配数组。

For heap-allocation after initialization, there's afaik no way around new, gcnew, malloc(),...

对于初始化后的堆分配,没有办法解决new,gcnew,malloc(),...

#1


You have to differ between initialization and assignment. It's like Tobias Wärre said in his post. You can't do (3) because assingnment does not work with the initialization brackets. (4) does work because you ardinarly assign the new values to your array. Actually the following should work:

初始化和分配之间必须有所不同。就像TobiasWärre在他的帖子中所说的那样。你不能这样做(3)因为assingnment不能用于初始化括号。 (4)确实有效,因为您将新值分配给数组。实际上以下应该有效:

public ref class decoder {
    array<System::Byte>^ css_keycode;
   ...
    decoder(void) {
        array<System::Byte>^ css_keycode_tmp = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
        css_keycode = css_keycode_tmp;
    }
}

This way the assigned values are copied to your array.

这样,指定的值将复制到您的数组中。

EDIT: Unfortunately there is no swap method like for STL containers (at least none I know of), else you could just swap the contents with the temporary.

编辑:不幸的是没有像STL容器那样的交换方法(至少我不知道),否则你可以用临时交换内容。

#2


You should be able to skip the size parameter when allocating the array with an aggregate initializer. For example, the following code compiles for me:

在使用聚合初始化程序分配数组时,您应该能够跳过size参数。例如,以下代码为我编译:

public ref class TestIt
{
public:
   TestIt()
   {
      mArray = gcnew cli::array<System::Byte>{0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};
   }

private:
   cli::array<System::Byte>^ mArray;

};

Your example (3) doesn't work because the gcnew array<type> is required.

您的示例(3)不起作用,因为需要gcnew数组

#3


It seems like if you want to initialize an array like that

好像你想要初始化这样的数组

(array = {elem1, elem2,etc})

you need to do it at declaration. The declaration would also only have local scope, i.e. you cannot use the memory safely when exiting the function that declares the array, in that case you need to allocate the memory using new. In the latter case, do not forget to delete your object when it is no longer needed by the application.

你需要在申报时这样做。声明也只有本地范围,即退出声明数组的函数时不能安全地使用内存,在这种情况下,您需要使用new分配内存。在后一种情况下,不要忘记在应用程序不再需要时删除您的对象。

So 1 if you only need it locally or during a function call from that specific function and 2 if you need it elsewhere when the function has exited.

因此,如果您只需要本地或在特定函数的函数调用期间使用它,如果您在函数退出时需要其它函数则需要2。

#4


I think you're stuck with (2).

我认为你坚持(2)。

In C99, you could actually use compound literals to do (3), but I don't know if there's something like this in C++/CLI. It wouldn't help with your problem, anyway: using a compound literal in the body of a function would stack-, not heap-allocate the array.

在C99中,你实际上可以使用复合文字来做(3),但我不知道在C ++ / CLI中是否有这样的东西。无论如何,它对你的问题没有帮助:在函数体中使用复合文字将堆栈,而不是堆分配数组。

For heap-allocation after initialization, there's afaik no way around new, gcnew, malloc(),...

对于初始化后的堆分配,没有办法解决new,gcnew,malloc(),...