Arrays of strings in Managed C++

拟墨画扇 提交于 2019-12-05 03:43:05

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

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

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

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

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).

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 :)

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

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