Thursday, January 19, 2006

Array var in Managed(.Net) C++

In managed C++, define an array variable for a managed class, in syntax, is different from standard C/C++. For example,


using System::Drawing;
//...
array<PointF, 1>^ pointsF = gcnew array<PointF, 1>(100);
// PointF pointsF[100]; // is wrong
// PointF pointsF[] = gcnew PointF[100]; // is wrong
//...
pointsF[0].X = 11.23f;
pointsF[0].Y = 0.54f;


where PointF is a structure defined in System::Drawing namespace.

For String class, you have define an array of String as:


array<String^, 1>^ s = gcnew array<String^, 1>(10);
//String^ s[] = gcnew String^[10]; // wrong again


The general format to define an array of type is:


[qualifiers] [cli::]array<[qualifiers]type1[, dimension]>^ var =
gcnew [cli::]array<type2[, dimension]> (val[,val...])


See MSDN on Array Keyword

0 comments: