数组的初始化在托管C ++ [英] Array initialization in Managed C++

查看:169
本文介绍了数组的初始化在托管C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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");

有两个问题:

  1. 这是更冗长
  2. 这要求我声明数组的大小前面,这是不方便对我的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"};

这篇关于数组的初始化在托管C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆