[ACCEPTED]-Arrays of strings in Managed C++-managed-c++

Accepted answer
Score: 11

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

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

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

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

gcroot<String^>[] unmanagedArray;

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

Score: 4


That should have all the answers you need 13 :)

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

Score: 1

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

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

More Related questions