托管 C++ 中的数组初始化
我希望声明并初始化一个一维托管项目数组。
如果它是 C# 代码,我会这样写:
VdbMethodInfo[] methods = new VdbMethodInfo[] {
new VdbMethodInfo("Method1"),
new VdbMethodInfo("Method2")
};
我正在尝试在托管 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");
有两个问题这:
- 它更详细
- 它要求我预先声明数组的大小,这对我的代码生成器来说很不方便
托管C++中的GC数组是否有“数组初始化”语法? 正确的语法是什么? 对于这个问题和其他类似问题,是否有一个好的网络链接?
I wish to declare and initialize a 1D managed array of items.
If it was C# code, I would write it like this:
VdbMethodInfo[] methods = new VdbMethodInfo[] {
new VdbMethodInfo("Method1"),
new VdbMethodInfo("Method2")
};
I am trying to write (well, actually, I'm writing a program generate) the same thing in managed C++...
So far I have:
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");
There are two problems with this:
- It's more verbose
- It requires me to declare the size of the array up front, which is inconvenient for my code generator
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
C++/CLI 数组声明 & 初始化语法与 C# 中的语法没有什么不同。 这是一个例子...
The C++/CLI array declare & initialize syntax is not dissimilar from that in C#. Here's an example...
有关托管阵列语法的 MSDN 页面:http://msdn. microsoft.com/en-us/library/ts4c4dw6(VS.80).aspx
MSDN page on managed array syntax: http://msdn.microsoft.com/en-us/library/ts4c4dw6(VS.80).aspx