Arrays of strings in Managed C++

Jonathan Prior picture Jonathan Prior · Jun 15, 2009 · Viewed 18.5k times · Source

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

String^ linet[];

throws an error

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

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

Answer

Niki picture Niki · Jun 15, 2009

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