C++ CLI Collection initializer syntax

你离开我真会死。 提交于 2020-01-02 08:46:08

问题


Is this supported in C++ CLI? I want to do something like the following C# example in C++ CLI

var dictionary = new Dictionary<string, string> { { "foo", "bar" } };

Thanks


回答1:


The best I came up with was creating an array initialized inline, then initializing the dictionary with the contents of the array in a static constructor. Something like

static initonly System::Collections::Generic::Dictionary<System::String^, System::String^>^ dictionary;
static initonly array<System::String^> arrayToPopulateDictionary = gcnew array<System::String^> { "foo", "bar" };

static Foo()
{
    for (int i  = 0; i < arrayToPopulateDictionary->Length; i += 2)
        listMappings->Add(arrayToPopulateDictionary[i], arrayToPopulateDictionary[i + 1]));
}



回答2:


Unless I'm horribly mistaken, this is simply not possible.




回答3:


It is definitely going to be possible in C++0x, whether that translates to C++/CLI or not is unknown (it should do).



来源:https://stackoverflow.com/questions/1923928/c-cli-collection-initializer-syntax

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!