Property Index Declaration
The syntax for declaring an indexed property has changed from Managed Extensions for C++ to Visual C++ 2008.
The two primary shortcoming of the Managed Extensions language support of indexed properties is the inability to provide class-level subscripting; that is, all indexed properties are required to be given a name, and thus there is no way, for example, to provide a managed subscript operator that can be directly applied to a Vector or Matrix class object. A second less significant shortcoming is that it is visually difficult to distinguish a property from an indexed property – the number of parameters is the only indication. Finally, indexed properties suffer from the same problems as those of non-indexed properties – the accessors are not treated as an atomic unit, but separated into individual methods. For example:
public __gc class Vector;
public __gc class Matrix {
float mat[,];
public:
__property void set_Item( int r, int c, float value);
__property float get_Item( int r, int c );
__property void set_Row( int r, Vector* value );
__property Vector* get_Row( int r );
};
As you can see here, the indexers are distinguished only by the additional parameters to specify a two or single dimension index. In the new syntax, the indexers are distinguished by the bracket ([,]) following the name of the indexer and indicating the number and type of each index:
public ref class Vector {};
public ref class Matrix {
private:
array<float, 2>^ mat;
public:
property float Item [int,int] {
float get( int r, int c );
void set( int r, int c, float value );
}
property Vector^ Row [int] {
Vector^ get( int r );
void set( int r, Vector^ value );
}
};
To indicate a class level indexer that can be applied directly to objects of the class in the new syntax, the default keyword is reused to substitute for an explicit name. For example:
public ref class Matrix {
private:
array<float, 2>^ mat;
public:
// ok: class level indexer now
//
// Matrix mat …
// mat[ 0, 0 ] = 1;
//
// invokes the set accessor of the default indexer …
property float default [int,int] {
float get( int r, int c );
void set( int r, int c, float value );
}
property Vector^ Row [int] {
Vector^ get( int r );
void set( int r, Vector^ value );
}
};
In the new syntax, when the default indexed property is specified, the two following names are reserved: get_Item and set_Item. This is because these are the underlying names generated for the default indexed property.
Note that there is no simple index syntax analogous to the simple property syntax.