c++ cli - What is the best way to initialize an array to a fixed-length array? (C++/CLI) -


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

   

array < System :: Byte & gt; ^ Css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};

or (2):

  array < System :: Byte & gt; ^ Css_keycode; Css_keycode = gcnew array & lt; System :: Byte & gt; (6) {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};  

But I can not do it apparently (3):

  array < System :: Byte & gt; ^ Css_keycode; Css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00};  

Even if I can (4):

  the array & lt; System :: Byte & gt; ^ Css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00}; Array & lt; System :: Byte & gt; ^ Css_keycode_shadow; Css_keycode_shadow = css_keycode;  

Is there any better way that I am missing? I would like a simple / clear way to write something like this:

  public ref class decoder {array < System :: Byte & gt; ^ Css_keycode; ... Decoder (zero) {css_keycode = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00}; }}  

Thanks!

You have to differentiate between initialization and the assignment is like Tobias warrant, on their post. You can not do this (3) because assnnment does not work with initialization bracket (4) works because you charge new values ​​for your array. In fact, the following should be done:

  public ref class decoder {array & lt; System :: Byte & gt; ^ Css_keycode; ... decoder (zero) {array & lt; System :: Byte & gt; ^ Css_keycode_tmp = {0x51, 0x67, 0x67, 0xc5, 0xe0, 0x00}; Css_keycode = css_keycode_tmp; }}  

The values ​​assigned in this manner are copied to your array.

Edit: Unfortunately, no swap method for STL containers (at least I do not know), otherwise you will have temporary content Can swap.


Comments