数组的初始化在托管C ++数组、初始化

2023-09-03 04:23:53 作者:沵不就仗著我喜歡沵嘛

我想声明并初始化项目的一维管理的阵列。

I wish to declare and initialize a 1D managed array of items.

如果是C#code,我会写这样的:

If it was C# code, I would write it like this:

VdbMethodInfo[] methods = new VdbMethodInfo[] {
    new VdbMethodInfo("Method1"),
    new VdbMethodInfo("Method2")
};

我想写(好吧,其实,我正在写一个程序生成)的同样的事情托管C ++ ...

I am trying to write (well, actually, I'm writing a program generate) the same thing in managed C++...

到目前为止,我有:

typedef array<VdbMethodInfo^, 1> MethodArray;
// How do I avoid pre-declaring the size of the array up front?
MethodArray^ methods = gcnew MethodArray(2);
methods[0] = gcnew VdbMethodInfo("Method1");
methods[1] = gcnew VdbMethodInfo("Method2");

有两个问题:

这是更冗长 这要求我声明数组的大小前面,这是不方便对我的code发电机

是否有托管C ++的数组初始化语法GC阵列?什么是正确的语法?是否有一个良好的网络链接,这和其他类似的问题?

Is there an "array initialization" syntax for GC arrays in Managed C++? What is the correct syntax? Is there a good web link for this and other similar questions?

推荐答案

在C ++ / CLI数组声明和放大器;初始化语法不从,在C#一样。下面是一个例子...

The C++/CLI array declare & initialize syntax is not dissimilar from that in C#. Here's an example...

array<String^>^ myArray = gcnew array<String^> {"first",  "second"};