C++怎样实现一个类的索引器(一)

2014-11-24 12:22:38 · 作者: · 浏览: 2

How to: Use Indexed Properties

An indexed property typically exposes a data structure that is accessed using a subscript operator.

A default indexed property allows the user to access the data structure via the class name, whereas a user-defined indexed property requires the user to specify the property name to access the data structure.

For information on accessing a default indexer via the this pointer, see Semantics of the this Pointer in Value and Reference Types.

For information on consuming an indexer authored in C#, see How to: Consume a C# Indexer.

Example
The following code sample shows how to use default and user defined indexed properties.

[cpp] // mcppv2_property_2.cpp
// compile with: /clr
using namespace System;
public ref class C {
array^ MyArr;

public:
C() {
MyArr = gcnew array(5);
}

// default indexer
property int default[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}

// user-defined indexer
property int indexer1[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
};

int main() {
C ^ MyC = gcnew C();

// use the default indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC[i] = i;
Console::Write("{0} ", MyC[i]);
}

Console::WriteLine("]");

// use the user-defined indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC->indexer1[i] = i * 2;
Console::Write("{0} ", MyC->indexer1[i]);
}

Console::WriteLine("]");
}
// mcppv2_property_2.cpp
// compile with: /clr
using namespace System;
public ref class C {
array^ MyArr;

public:
C() {
MyArr = gcnew array(5);
}

// default indexer
property int default[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}

// user-defined indexer
property int indexer1[int] {
int get(int index) {
return MyArr[index];
}
void set(int index, int value) {
MyArr[index] = value;
}
}
};

int main() {
C ^ MyC = gcnew C();

// use the default indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC[i] = i;
Console::Write("{0} ", MyC[i]);
}

Console::WriteLine("]");

// use the user-defined indexer
Console::Write("[ ");
for (int i = 0 ; i < 5 ; i++) {
MyC->indexer1[i] = i * 2;
Console::Write("{0} ", MyC->indexer1[i]);
}

Console::WriteLine("]");
}

Output
[ 0 1 2 3 4 ] [ 0 2 4 6 8 ]
Example
This sample shows how to call the default indexer through the this pointer.

[cpp] // call_default_indexer_through_this_pointer.cpp
// compile with: /clr /c
value class Position {
public:
Position(int x, int y) : position(gcnew array(100, 100)) {
this->default[x, y] = 1;
}

property int default[int, int] {
int get(int x, int y) {
return position[x, y];
}

void set(int x, int y, int value) {}
}

private:
array ^ position;
};
// call_default_indexer_through_this_pointer.cpp
// compile with: /clr /c
value class Position {
public:
Position(int x, int y) : position(gcnew array(100, 100)) {
this->default[x, y] = 1;
}

property int default[int, int] {
int get(int x, int y) {
return position[x, y];
}

void set(int x, int y, int value) {}
}

private:
array ^ pos