views:

634

answers:

4

In managed C++/CLI, I could do this either as (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):

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

even though I can do (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!

A: 

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.

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.

Tobias Wärre
A: 

I think you're stuck with (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.

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

Christoph
A: 

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:

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.

Nick Desjardins
+1  A: 

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:

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.

fmuecke