Array of strings in managed C ++

I am trying to write an application in Managed C ++, but I cannot decide how to declare an array of strings.

String ^ linet [];

gives an error message

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

Therefore, I believe that for managed data types there is another way to do this. What it is?

+6
c ++ arrays managed managed-c ++
source share
3 answers

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

Assuming you are actually using the C ++ / CLI (due to 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. same type as string [] in C #.

 gcroot<String^>[] unmanagedArray; 

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

+7
source share

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

This should have all the necessary answers :)

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 comes from System :: Object) can only be used in a controlled context. Standard C ++ array basically creates a fixed-size block of memory on the heap, with sizeof (type) x NumberOfItems bytes, and then iterates through it. A managed type cannot be guaranteed to remain the same place on the heap since it was originally, so you cannot do this :)

+4
source share

You are using the collection class from .Net. For example :

 List<String^>^ dinosaurs = gcnew List<String^>(); 
+1
source share

All Articles