Managed C ++中的字符串数组

时间:2022-09-01 11:27:57

I'm trying to write an application in Managed C++, but I cannot work out how to declare an array of strings.

我正在尝试用Managed C ++编写一个应用程序,但我无法弄清楚如何声明一个字符串数组。

String^ linet[];

throws an error

抛出错误

'System::String ^' : a native array cannot contain this managed type

'System :: String ^':本机数组不能包含此托管类型

So I suppose there's a different way to do this for managed data types. What exactly is it?

所以我认为对托管数据类型有不同的方法。究竟是什么?

3 个解决方案

#1


7  

Do you really mean Managed C++? Not C++/CLI?

你真的是指Managed C ++吗?不是C ++ / CLI?

Assuming you're actually using C++/CLI (because of the error message you posted), there are two ways to do this:

假设您实际使用的是C ++ / CLI(由于您发布的错误消息),有两种方法可以执行此操作:

array<String^>^ managedArray = gcnew array<String^>(10);

will create a managed array, i.e. the same type as string[] in C#.

将创建一个托管数组,即与C#中string []相同的类型。

gcroot<String^>[] unmanagedArray;

will create an unmanaged C++ array (I've never actually tried this with arrays - it works well with stl containers, so it should work here, too).

将创建一个非托管C ++数组(我从来没有真正尝试过使用数组 - 它适用于stl容器,因此它也可以在这里工作)。

#2


4  

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx

That should have all the answers you need :)

那应该有你需要的所有答案:)

When working with Managed C++ (aka. C++/CLI aka. C++/CLR) you need to consider your variable types in everything you do. Any "managed" type (basically, everything that derives from System::Object) can only be used in a managed context. A standard C++ array basically creates a fixed-size memory-block on the heap, with sizeof(type) x NumberOfItems bytes, and then iterates through this. A managed type can not be guarenteed to stay the same place on the heap as it originally was, which is why you can't do that :)

使用托管C ++(又名.C ++ / CLI又名.C ++ / CLR)时,您需要在所做的每件事情中考虑变量类型。任何“托管”类型(基本上,派生自System :: Object的所有内容)只能在托管上下文中使用。标准C ++数组基本上在堆上创建一个固定大小的内存块,使用sizeof(type)x NumberOfItems字节,然后遍历这个。托管类型无法保证在堆上保持原来的位置,这就是为什么你不能这样做:)

#3


1  

You use a collection class from .Net. For example:

您使用.Net的集合类。例如:

List<String^>^ dinosaurs = gcnew List<String^>();

#1


7  

Do you really mean Managed C++? Not C++/CLI?

你真的是指Managed C ++吗?不是C ++ / CLI?

Assuming you're actually using C++/CLI (because of the error message you posted), there are two ways to do this:

假设您实际使用的是C ++ / CLI(由于您发布的错误消息),有两种方法可以执行此操作:

array<String^>^ managedArray = gcnew array<String^>(10);

will create a managed array, i.e. the same type as string[] in C#.

将创建一个托管数组,即与C#中string []相同的类型。

gcroot<String^>[] unmanagedArray;

will create an unmanaged C++ array (I've never actually tried this with arrays - it works well with stl containers, so it should work here, too).

将创建一个非托管C ++数组(我从来没有真正尝试过使用数组 - 它适用于stl容器,因此它也可以在这里工作)。

#2


4  

http://www.codeproject.com/KB/mcpp/cppcliarrays.aspx

That should have all the answers you need :)

那应该有你需要的所有答案:)

When working with Managed C++ (aka. C++/CLI aka. C++/CLR) you need to consider your variable types in everything you do. Any "managed" type (basically, everything that derives from System::Object) can only be used in a managed context. A standard C++ array basically creates a fixed-size memory-block on the heap, with sizeof(type) x NumberOfItems bytes, and then iterates through this. A managed type can not be guarenteed to stay the same place on the heap as it originally was, which is why you can't do that :)

使用托管C ++(又名.C ++ / CLI又名.C ++ / CLR)时,您需要在所做的每件事情中考虑变量类型。任何“托管”类型(基本上,派生自System :: Object的所有内容)只能在托管上下文中使用。标准C ++数组基本上在堆上创建一个固定大小的内存块,使用sizeof(type)x NumberOfItems字节,然后遍历这个。托管类型无法保证在堆上保持原来的位置,这就是为什么你不能这样做:)

#3


1  

You use a collection class from .Net. For example:

您使用.Net的集合类。例如:

List<String^>^ dinosaurs = gcnew List<String^>();